Click here to Skip to main content
15,892,161 members
Articles / Programming Languages / C

The Beginner's Guide to Using Enum Flags

Rate me:
Please Sign up or sign in to vote.
3.54/5 (97 votes)
10 Jan 2009CPOL6 min read 311.4K   608   93  
An article explaining the rudiments of how to deal with Flags in C++
/* FlagsGuide.cpp
 *
 * Copyright (c) 2006 by toxcct. All rights reserved.
 * Consult your license regarding permissions and restrictions.
 */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>


enum Flags {
	STYLE1 =   1,
	STYLE2 =   2,
	STYLE3 =   4,
	STYLE4 =   8,
	STYLE5 =  16,
	STYLE6 =  32,
	STYLE7 =  64,
	STYLE8 = 128
};

void main(void) {
	char pszStyle1Binary[65];
	char pszStyle2Binary[65];
	char pszStyle3Binary[65];
	char pszStyle4Binary[65];
	char pszStyle5Binary[65];
	char pszStyle6Binary[65];
	char pszStyle7Binary[65];
	char pszStyle8Binary[65];
	itoa(STYLE1, pszStyle1Binary, 2);
	itoa(STYLE2, pszStyle2Binary, 2);
	itoa(STYLE3, pszStyle3Binary, 2);
	itoa(STYLE4, pszStyle4Binary, 2);
	itoa(STYLE5, pszStyle5Binary, 2);
	itoa(STYLE6, pszStyle6Binary, 2);
	itoa(STYLE7, pszStyle7Binary, 2);
	itoa(STYLE8, pszStyle8Binary, 2);
	printf("\n\n================[ FLAGS ]================\n"
		   " STYLE1 = %3d = 0x%02X = 0b%08s\n"
		   " STYLE2 = %3d = 0x%02X = 0b%08s\n"
		   " STYLE3 = %3d = 0x%02X = 0b%08s\n"
		   " STYLE4 = %3d = 0x%02X = 0b%08s\n"
		   " STYLE5 = %3d = 0x%02X = 0b%08s\n"
		   " STYLE6 = %3d = 0x%02X = 0b%08s\n"
		   " STYLE7 = %3d = 0x%02X = 0b%08s\n"
		   " STYLE8 = %3d = 0x%02X = 0b%08s\n\n",
		   STYLE1, STYLE1, pszStyle1Binary,
		   STYLE2, STYLE2, pszStyle2Binary,
		   STYLE3, STYLE3, pszStyle3Binary,
		   STYLE4, STYLE4, pszStyle4Binary,
		   STYLE5, STYLE5, pszStyle5Binary,
		   STYLE6, STYLE6, pszStyle6Binary,
		   STYLE7, STYLE7, pszStyle7Binary,
		   STYLE8, STYLE8, pszStyle8Binary);
	getch();


	int iMyStyle = STYLE1 | STYLE3 | STYLE6;
	char pszMyStyleBinary[65];
	itoa(iMyStyle, pszMyStyleBinary, 2);
	printf("\n\n===============[ MY STYLE ]==============\n"
		   " iMyStyle = STYLE1 | STYLE3 | STYLE6\n"
		   "          = 0b%08s\n\n",
		   pszMyStyleBinary);
	getch();


	printf("\n\n============[ Bit Extraction ]===========\n"
		   " Bit 5 = (iMyStyle & STYLE5) == STYLE5\n"
		   " Bit 5 = %s\n"
		   " Bit 6 = (iMyStyle & STYLE6) == STYLE6\n"
		   " Bit 6 = %s\n\n",
		   (iMyStyle & STYLE5) == STYLE5 ? "true" : "false",
		   (iMyStyle & STYLE6) == STYLE6 ? "true" : "false");
	getch();


	printf("\n\n==============[ Add Style 8 ]============\n"
		   " iMyStyle before = 0b%08s\n"
		   " Bit 8 before    =   %d\n\n",
		   pszMyStyleBinary,
		   (iMyStyle & STYLE8) == STYLE8 ? 1 : 0);
	printf("     -> Adding STYLE8 (2 ways):\n"
		   "            -> iMyStyle = iMyStyle | STYLE8\n"
		   "            -> iMyStyle |= STYLE8\n\n");
	iMyStyle |= STYLE8;
	itoa(iMyStyle, pszMyStyleBinary, 2);
	printf(" iMyStyle after = 0b%08s\n"
		   " Bit 8 after    =   %d\n\n",
		   pszMyStyleBinary,
		   (iMyStyle & STYLE8) == STYLE8 ? 1 : 0);
	getch();


	printf("\n\n===========[ Removing Style 8 ]==========\n"
		   " iMyStyle before = 0b%08s\n"
		   " Bit 8 before    =   %d\n\n",
		   pszMyStyleBinary,
		   (iMyStyle & STYLE8) == STYLE8 ? 1 : 0);
	printf("     -> Removing STYLE8 (2 ways):\n"
		   "            -> iMyStyle = iMyStyle & ~STYLE8\n"
		   "            -> iMyStyle &= ~STYLE8\n\n");
	iMyStyle &= ~STYLE8;
	itoa(iMyStyle, pszMyStyleBinary, 2);
	printf(" iMyStyle after = 0b%08s\n"
		   " Bit 8 after    =   %d\n\n",
		   pszMyStyleBinary,
		   (iMyStyle & STYLE8) == STYLE8 ? 1 : 0);
	getch();


	printf("\n\n===========[ Switching Style 6 ]==========\n"
		   " iMyStyle before = 0b%08s\n"
		   " Bit 6 before    =     %d\n\n",
		   pszMyStyleBinary,
		   (iMyStyle & STYLE6) == STYLE6 ? 1 : 0);
	printf("     -> Switching STYLE6 (2 ways) 1st time:\n"
		   "            -> iMyStyle = iMyStyle ^ STYLE6\n"
		   "            -> iMyStyle ^= STYLE6\n\n");
	iMyStyle ^= STYLE6;
	itoa(iMyStyle, pszMyStyleBinary, 2);
	printf(" iMyStyle after = 0b%08s\n"
		   " Bit 6 after    =     %d\n\n",
		   pszMyStyleBinary,
		   (iMyStyle & STYLE6) == STYLE6 ? 1 : 0);
	printf("     -> Switching STYLE6 2nd time\n\n");
	iMyStyle ^= STYLE6;
	itoa(iMyStyle, pszMyStyleBinary, 2);
	printf(" iMyStyle after = 0b%08s\n"
		   " Bit 6 after    =     %d\n\n",
		   pszMyStyleBinary,
		   (iMyStyle & STYLE6) == STYLE6 ? 1 : 0);


	printf("\n\n=================[ END ]=================\n\n");
	getch();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Accenture Technology Solutions
France France

Toxcct is an electronics guy who felt in love with programming at the age of 10 when he discovered C to play with Texas-Instruments calculators.

Few years later, he discovered "The C++ Language" from Bjarne Stroustrup ; a true transformation in his life.

Now, toxcct is experiencing the Web by developing Siebel CRM Applications for a living. He also respects very much the Web Standards (YES, a HTML/CSS code MUST validate !), and plays around with HTML/CSS/Javascript/Ajax/PHP and such.

_____

After four years of services as a Codeproject MVP, toxcct is now taking some distance as he doesn't like how things are going on the forums. he particularly doesn't accept how some totally ignorant people got the MVP Reward by only being arrogant and insulting while replying on the technical forums.



Comments and Discussions