Thread: Function displaying bits...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Function displaying bits...

    Hi,

    Since C has no standard way to display the bits of a number, my book has the following function:

    Code:
    void displayBits( unsigned value )
    { 
       unsigned c, displayMask = 1 << 31;
    
       printf( "%7u = ", value );
    
       for ( c = 1; c <= 32; c++ ) { 
          putchar( value & displayMask ? '1' : '0' );
          value <<= 1;
    
          if ( c % 8 == 0 )
             putchar( ' ' );
       }
    
       putchar( '\n' );
    }
    WHat i don't understand is that why it has to use the AND operator, why is the mask needed and why does it need to be
    1 << 31, can it be anything else?

    thnx

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    > 1 << 31, can it be anything else?

    well yeah it could be

    displayMask = 2147483647 ;

    1 << 31,

    what this does is shift the bit 1 to the last bit this is for obviously a mask,

    putchar( value & displayMask ? '1' : '0' );

    whats this is doing is testing it this particular bit of "value" is set to one then display a 1 if not display a zero

    the & compares thw bits of Mask

    displayMask in binary:
    10000000 00000000 00000000 00000000

    &

    a "value" of 2147483652 binary:
    10000000 00000000 00000000 00000101
    __________________________________

    10000000 00000000 00000000 00000000

    after each test

    value <<= 1;

    shifts all the bits one to the left so that each bit can be tested individually,

    hence "value"

    10000000 00000000 00000000 00000101

    <<= 1
    __________________________________

    00000000 00000000 00000000 00001010

    with each bit shifted 1 to the left,

    it continues this 32 time (one time for each bit) and printing the result wich will display the number in binary.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    but why choose 1<< 31 ?

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    its prettier and look more profession and confusing to the newbs.
    hehe.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    but doesn't the mask will directly affect the output sinze it's used to AND the value passed to the function ?

    therefore choosing a different mask will output a differnt result?

    and say if the value is 65000, and the displayMask you said is 2147483652, then 65000 and 2147483652 must both converted to binary number to AND each other right? SO does it mean the C automatically converts them into binary number and the outputs either the character '1' or '0'. But we'll need a self-wrote function to actuall display the results?


    pls help, i admint i'm a newb to this..
    Last edited by Nutshell; 01-26-2002 at 10:43 PM.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    wait i just tried running the program with different masks. They all output the same binary number. SO can i concluded that no matter what number i use for the mask, the result outputted when the mask ANDed with a value will be the correct binary number?

    but i still wanna know if C automtically converts them to binary numbers before ANDing them..

    But thats somethin strange. I tried the following:
    Code:
    00000000 00000000 11111101 11101000 - 65000
    10000000 00000000 00000000 00000000 - 2147483648
    -----------------------------------
    00000000 00000000 00000000 00000000 - 0
    When they're ANDed it becomes 0. I'm totally confused pls give a hand and do some explaining.

    thnx
    Last edited by Nutshell; 01-26-2002 at 10:54 PM.

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    sure thing,

    >
    SO can i concluded that no matter what number i use for the mask, the result outputted when the mask ANDed with a value will be the correct binary number?
    <

    actually incorrect, if you chose a number greater than the masks value i doubt it would work.

    >but i still wanna know if C automtically converts them to binary numbers before ANDing them..

    all numbers in computers are handled at the lowest level in binary.

    ok AND(&) works like so,

    if the two bits being compared are both 1 then the resulting bit is 1 otherwise its always 0

    it would be like so

    Code:
    00000000 00000000 00000000 00000101    '5'
    &
    00000000 00000000 00000000 00101001   '41'
    _____________________________________
    00000000 00000000 00000000 00000001    '1'
    since only the first bit of both numbers is set(1) then only the first bit in the resulting number will be 1

    another example would be

    Code:
    00000000 00000000 00000000 01101000  '104'
    &
    00000000 00000000 00000000 10101001  '169'
    ______________________________________
    00000000 00000000 00000000 00101000   '40'
    don't worry it took me a while to get it too.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok, thnx , but i still don't quite understand how the function works. As one last sstep, i kindly ask you to add some comments in the code, or the steps the function take to perform its tasks to help me understand more.

    thnx in advance

  9. #9
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    sure... ill try

    Code:
    void displayBits( unsigned value )
    { 
       unsigned c, displayMask = 1 << 31; // set the bit mask so that we only compare the last bit
    
       printf( "%7u = ", value ); 
    
       // loop 32 time or the number of bits in a type unsigned 
       for ( c = 1; c <= 32; c++ ) { 
       // now we use the mask to test the last bit of "value" and see if its set(1) if it is putchar a 1 other wise a 0
          putchar( value & displayMask ? '1' : '0' );
          value <<= 1; // move the bits of "value one to the left i'll explain bit shifting below
          // for ever eight bit(a byte) put a space to seperate.
          if ( c % 8 == 0 )
             putchar( ' ' );
       }
    
       putchar( '\n' );
    }
    bit shifting works like so

    say i have the value
    Code:
    00000000 00000000 00000000 00000001  '1'
    if is shift it one bit to the left (eg. "value <<= 1") i would get
    00000000 00000000 00000000 00000010  '2'
    lets say i shift in another ten bits "value <<= 10" i would get
    00000000 00000000 00001000 00000000  '4096'
    
    but what happens if i shift past the ends?
    
    that bit is lost.
    so if i shifted
    
    10000000
    
    left once (<< 1)
    
    00000000
    
    would be the result
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  10. #10
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    okok one last last question on this. I modify the existing piece of code to print out what is happening below:

    Code:
    #include <stdio.h>
    
    void displayBits2( unsigned );
    void displayBits( unsigned );
    
    int main()
    { 
       unsigned x;
    
       printf( "Enter an unsigned integer: " );
       scanf( "%u", &x );
       displayBits( x );
       getch();
       return 0;
    }
    
    void displayBits( unsigned value )
    { 
       unsigned c, displayMask = 1 << 31;
    
       printf( "%7u = ", value );
    
       for ( c = 1; c <= 32; c++ ) { 
          printf( "\n\nvalue is currently: \n" );
          displayBits2( value );
          printf( " Mask is currently: \n" );
          displayBits2( displayMask );
          printf( "Output value after comparing: " );
          putchar( value & displayMask ? '1' : '0' );
          printf( "\n" );
          value <<= 1;
    
          if ( c % 8 == 0 )
             putchar( ' ' );
       }
    
       putchar( '\n' );
    }
    void displayBits2( unsigned value )
    { 
       unsigned c, displayMask = 1 << 31;
    
       printf( "%7u = ", value );
    
       for ( c = 1; c <= 32; c++ ) { 
          putchar( value & displayMask ? '1' : '0' );
    
          value <<= 1;
    
          if ( c % 8 == 0 )
             putchar( ' ' );
       }
    
       putchar( '\n' );
    }
    But i am not sure what the value is currently up to when it gets shifted. No, i should say that i am not sure which two bits are actually being compared. Say the number 3 has the binary equilvalent of 11. But is the first one being compared or the 2nd one? I need to get clear of that.

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Hey i think i figured out!

    Is it like the initial value that it's used to compare is the rightmost value which is not a zero. Then it starts shifting to the left side and when the rightmost value is shifted pass the end then it starts comparing the value right after it?

    am i right?

    If you think that i don't know seomthing pls tell me and point me to the right direction, it'll be appreciated.

    thnx, especially to no-one

  12. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >am i right?

    from your explaination i think you may have it.

    Well if you have any more questions just ask the majority of the people here are quite good.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM