Thread: Bit Flags

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Bit Flags

    I've been breezing through my C book and having fun while doing it. Today, I reached the Binary, Hex, and Dec section of my book. I understood it all well except a section about bit flags. The book says


    A common use for the bitwise operators is to combine several values in a single variable for efficiency. For instance, a program with several "flag" variables, which always have a value of either 1 or 0 (representing true or false states), would normally require eight bits of memory each. The storage of a single digit only requires a single bit, however, so up to eight flags can be combined in a single variable, as seen with the "state" variable in this example:
    Code:
    #include <stdio.h>
    #define FLAG_1	1	/* 0000 0001 */
    #define FLAG_2	2	/* 0000 0010 */
    #define FLAG_3	4	/* 0000 0100 */
    #define FLAG_4	8	/* 0000 1000 */
    #define FLAG_5	16	/* 0001 0000 */
    #define FLAG_6	32	/* 0010 0000 */
    #define FLAG_7	64	/* 0100 0000 */
    #define FLAG_8	128	/* 1000 0000 */
    
    int main()
    {
    	int state = 44;	/* 0010 1100 */
    	if ((state & FLAG_1) > 0) printf("Flag 1 is set\n");
    	if ((state & FLAG_2) > 0) printf("Flag 2 is set\n");
    	if ((state & FLAG_3) > 0) printf("Flag 3 is set\n");
    	if ((state & FLAG_4) > 0) printf("Flag 4 is set\n");
    	if ((state & FLAG_5) > 0) printf("Flag 5 is set\n");
    	if ((state & FLAG_6) > 0) printf("Flag 6 is set\n");
    	if ((state & FLAG_7) > 0) printf("Flag 7 is set\n");
    	if ((state & FLAG_8) > 0) printf("Flag 8 is set\n");
    	return 0;
    }
    It then states:
    The flag constants have values of increasing powers of 2 to represent individual binary numbers which do not overlap. The state variable value contains an integer total of those flags which are set. For instance, if the state value is 1, flag 1 is set, if the state value is 3, both flags 1 and 2 are set, etc...
    The part I don't understand is that the code above prints this:
    Flag 3 is set
    Flag 4 is set
    Flag 6 is set
    But 3 + 4 + 6 is not 44. I thought the book said that "the state variable contains an integer total of those flags which are set." Can someone please clarify. Also, what exactly what you want to use this for? Thanks. Much appreciated.


    All quoted material is copyright Computer Step 2004 from the book C Programming in Easy Steps by Mike McGrath
    Last edited by Padawan; 03-30-2004 at 09:33 PM.

  2. #2
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Ive commented the code
    Code:
    #include <stdio.h>
    #define FLAG_1	1	/* 0000 0001 */
    #define FLAG_2	2	/* 0000 0010 */
    #define FLAG_3	4	/* 0000 0100 */
    #define FLAG_4	8	/* 0000 1000 */
    #define FLAG_5	16	/* 0001 0000 */
    #define FLAG_6	32	/* 0010 0000 */
    #define FLAG_7	64	/* 0100 0000 */
    #define FLAG_8	128	/* 1000 0000 */
    
    int main()
    {
    	int state = 44;	/* 0010 1100 */
    
            // does state's binary value have '1' at the end?
            // flag_1 is 0000 0001, and state is 0010 1100,
            // so the answer is no
    	if ((state & FLAG_1) > 0)
            {
                    printf("Flag 1 is set\n");
            }
    	if ((state & FLAG_2) > 0) printf("Flag 2 is set\n");
    	if ((state & FLAG_3) > 0) printf("Flag 3 is set\n");
    	if ((state & FLAG_4) > 0) printf("Flag 4 is set\n");
    	if ((state & FLAG_5) > 0) printf("Flag 5 is set\n");
    	if ((state & FLAG_6) > 0) printf("Flag 6 is set\n");
    	if ((state & FLAG_7) > 0) printf("Flag 7 is set\n");
    	if ((state & FLAG_8) > 0) printf("Flag 8 is set\n");
    	return 0;
    }
    I hope that this help (and is correct!)

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    2^2 + 2^3 + 2^5 = 44

    Each bit position represents a power of 2. The least significant bit is bit 0.

    gg

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Here's an example of a common usage
    Code:
    #include <stdio.h>
    
    #define say_hello 1 /* 0001 */
    #define say_goodbye 2 /* 0010 */
    
    int main()
    {
        int message = say_hello | say_goodbye; /* 0011 */
    
        if(message & say_hello)
        {
            printf("Hello!\n");
        }
        if(message & say_goodbye)
        {
            printf("Good-bye");
        }
        return 0;
    }
    it should output:

    Hello!
    Good-bye

    Get it?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Also, what exactly what you want to use this for?
    In this particular use, memory is saved by using a single integral type to represent multiple boolean values.
    Another words, a single byte provides storage for 8 boolean values.

    gg

  6. #6
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    windows uses it to find out which kind of window u want to make:

    WS_VISIBLE | WS_CLOSBOX ...

    etc.

    Each of those are '#define' s with different values in different spots: windows uses hex, instead of binary (to be read easier).

  7. #7
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ok I understand how it is used. But for some reason I am still not making the connection between:

    2^2 + 2^3 + 2^5 = 44
    And the fact that it prints:
    Flag 3 is set
    Flag 4 is set
    Flag 6 is set

  8. #8
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Codeplug:

    out of curiosity, what is your avatar a picture of??

  9. #9
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Code:
          |
    0000 0100 FLAG 3
    0010 1100 state : match ups the 1's (prints)
    
         |
    0000 1000 FLAG 4
    0010 1100 state : match ups the 1's (prints)
    
       |
    0001 0000 FLAG 5
    0010 1100 state : does not match up the 1's (doesn't print)

  10. #10
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    It makes it a lot easier if you just learn hex
    0x00 == 0000 0000
    0x01 == 0000 0001
    0x02 == 0000 0010
    0x03 == 0000 0011
    ...
    0xA0 == 1010 0000
    ...
    0xC0 == 1100 0000

    Makes it a whole lot easier to convert from readable hex to binary, unlike decimal where it is nearly impossible to tell what the bits are going to look like.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> what is your avatar a picture of??

    That's me eating some delicious corn.

    gg

  12. #12
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Quote Originally Posted by kinghajj
    Code:
          |
    0000 0100 FLAG 3
    0010 1100 state : match ups the 1's (prints)
    
         |
    0000 1000 FLAG 4
    0010 1100 state : match ups the 1's (prints)
    
       |
    0001 0000 FLAG 5
    0010 1100 state : does not match up the 1's (doesn't print)
    Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh. Thank you so much!!!!!!!!!!

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> But for some reason I am still not making the connection between:
    Code:
    Flag 3 is set
    Flag 4 is set
    Flag 6 is set 
    
    2^(3-1) + 2^(4-1) + 2^(6-1) = 44
    gg

  14. #14
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ok last question about this... I see how it selected the flags but how exactly does it save memory? Constants take up memory, correct?

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> but how exactly does it save memory? Constants take up memory, correct?
    Well, in the since that those constants are in the binary code of the application - yes.

    The simple example is this:
    You can use a single byte to represent 8 boolean values, one for each bit. Where a 0 bit is false, and a 1 bit is true.
    Or you can use an 8 byte array, where each byte contains a 1 for true, or 0 for false.

    So here's what you have to choose from:
    Code:
    char flags; // one byte of storage, 8 bits, one for each flag
    ...
    char flags[8]; // eight bytes of storage, one byte for each flag
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 64 bit testing
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2007, 03:20 AM
  2. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  4. Flags, the ambiguity of
    By Jeremy G in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2003, 11:41 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM