Thread: Need comment

  1. #1
    Unregistered
    Guest

    Question Need comment

    I am a beginner of C, I having the following example, but I don't understand the line

    1) "int MASK = 1<<(count-1);" and
    2) "while(count--)
    {
    printf("%d", ( byte & MASK ) ? 1 : 0 );
    byte <<= 1;
    }"

    Can you please expain to me. Thanks a lot.

    ************************************************** **

    #include <stdio.h>
    void binary_op( int byte );
    main()
    {
    int byte=55;
    binary_op( byte );
    }
    void binary_op( int byte )
    {
    int count=8;
    int MASK = 1<<(count-1);
    while(count--)
    {
    printf("%d", ( byte & MASK ) ? 1 : 0 );
    byte <<= 1;
    }
    printf("\n");
    }

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Well, if you are a beginner, these lines are code could be difficult. I'll touch upon them.

    > 1) "int MASK = 1<<(count-1);" and
    2) "while(count--)

    Okay, line #1 is doing a bitshift to assign the value to MASK. And line 2 is testing to see if count is not 0 yet. If it is, then the while loop does not continue. Understand?

    --Garfield

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    This is a way to print out a byte in binary. Since an int is more than one byte (usually two bytes or four bytes), the function can be easily changed to print out the whole int (change count=8 to count = 8*sizeof(int). If you know hexadecimal, it's easier to see by changing byte to various values like this:

    int byte=0xff;
    //int byte=0x80;
    //int byte=0x77;


    Code:
    void binary_op( int byte ) 
    { 
    int count=8;
    int MASK = 1<<(count-1);  //shift a one into the 8th bit position
    while(count--)            //loop 8 times
    { 
       printf("%d", ( byte & MASK ) ? 1 : 0 );
       //If there is a 1 at the bit position where mask has a 1, print a 1, else print a 0.
       //So this prints starting with the most significant bit.
       byte <<= 1;            //Check the next bit
    } 
    printf("\n"); 
    }
    Last edited by swoopy; 10-24-2001 at 01:50 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    for(x=0;x<8;x++)printf("%d",((byte&(1<<x))?1:0) );

    Quzah.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > for(x=0;x<8;x++)printf("%d",((byte&(1<<x))?1:0) );

    Wow ::staring in awe:: that's pretty good to change all of that into...this. Niiiiice.

    --Garfield
    1978 Silver Anniversary Corvette

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    for(x=0;x<8;x++)printf("%d",((byte&(1<<x))?1:0) );

    doesn't that print the bits the wrong way round??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For a single byte it doesn't matter. It only matters what way you decide to view it. Multi byte variables (int / long) have an "endian" which is different on compilers.

    It doesn't matter what way you display a single byte so long as you know how it's working. My display is "least significant bit first".

    You could just as easily do:

    for(x=7;x>-1;x--)printf("%d",((byte&(1<<x))?1:0) );

    Most significant bit first.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone clarify this comment.
    By Overworked_PhD in forum C Programming
    Replies: 2
    Last Post: 05-15-2007, 10:17 PM
  2. Comment using // or /* &&& */
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 03-16-2005, 02:45 PM
  3. Tab->Space converter and comment colorizer
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-30-2005, 05:46 PM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. what is a pragma comment?
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 11-25-2002, 05:50 AM