Thread: An integer with one bit turned on

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    An integer with one bit turned on

    Hi!

    Here is the quotation from a book called C pitfalls:

    Suppose that the defined constant FLAG is an integer with
    exactly one bit turned on in its binary representation (in other words, a power of two), and you want to test whether the integer variable flags has that bit turned on. The usual way to write this is:
    if (flags & FLAG) ...
    I am new to CS and programming. Please, explain what does it mean:
    an integer with exactly one bit turned on?
    What value does FLAG have in such case? I know that char is 1 bit,
    but I assume the FLAG is defined in the following way:

    Code:
     #define FLAG somevalue
    What value might it be?
    Thanks!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You seem to have mixed up "bit" and "byte". A "bit" can have two possible values( 0 or 1, off or on, false or true, whatever ). A "byte" is a collection of 8 consecutive bits. A char is one byte in most systems, but the standard says not to count on that.

    Now, about your question. A bit can't be addressed by its own, because it has no distinctive address. A byte, on the other hand, does. When you want to check whether a specific bit is 0 or 1 you simply AND with only that bit set to 1. The powers of two all have just the bit you want set: 1 2 4 8 16 32 64 128
    You can do the same with more than just bytes, of course. All the integral types can be used like this.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Quote Originally Posted by GReaper View Post
    You seem to have mixed up "bit" and "byte". A "bit" can have two possible values( 0 or 1, off or on, false or true, whatever ). A "byte" is a collection of 8 consecutive bits. A char is one byte in most systems, but the standard says not to count on that.

    Now, about your question. A bit can't be addressed by its own, because it has no distinctive address. A byte, on the other hand, does. When you want to check whether a specific bit is 0 or 1 you simply AND with only that bit set to 1. The powers of two all have just the bit you want set: 1 2 4 8 16 32 64 128
    You can do the same with more than just bytes, of course. All the integral types can be used like this.
    Thanks a lot for your answer. Just a little question:
    to check whether a specific bit is 0 or 1 you simply AND with only that bit set to 1.
    I don't understand the part saying "with only that bit set to 1".

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Think of an integer as a sequence of bits. For example, 42 would be 101010. When you are ANDing two integers, you do the logical AND operation for each of their bits, separately. For instance, (42 AND 24) equals (101010 AND 011000) which equals 001000(8). See it in vertical format:
    Code:
        101010
    AND 011000
        ------
        001000
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Quote Originally Posted by GReaper View Post
    Think of an integer as a sequence of bits. For example, 42 would be 101010. When you are ANDing two integers, you do the logical AND operation for each of their bits, separately. For instance, (42 AND 24) equals (101010 AND 011000) which equals 001000(8). See it in vertical format:
    Code:
        101010
    AND 011000
        ------
        001000
    Thank you )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gang-leader turned Mayor: Only in Hartford
    By Leiarchy9 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-06-2004, 01:03 PM
  2. Turned it down
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 11-03-2003, 07:55 PM
  3. my map is showing up 90 degrees turned!
    By frenchfry164 in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2002, 02:32 PM
  4. Replies: 5
    Last Post: 12-17-2001, 11:59 AM
  5. My brain has turned to an icky, gooey mush.
    By shark_boy in forum C++ Programming
    Replies: 1
    Last Post: 09-20-2001, 04:05 PM

Tags for this Thread