Thread: Bit Flags

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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