Last active
July 17, 2016 22:08
-
-
Save otaks/15f69b66b861c7d9ccbbc6f2cec91058 to your computer and use it in GitHub Desktop.
bitFlag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
//ビットフラグ定義 | |
#define FLAG_A 0x0001 | |
#define FLAG_B 0x0002 | |
#define FLAG_C 0x0004 | |
//ビットフラグ操作関数 | |
#define ON_FLAG(X,Y) (X |= Y) | |
#define OFF_FLAG(X,Y) (X &= ~Y) | |
#define GET_FLAG(X,Y) (X & Y) | |
void printFlag( unsigned int f ) { | |
char buf[ 32 ] = { 0 }; | |
itoa( f, buf, 2 ); | |
printf( "%032s\n", buf ); | |
} | |
int main() { | |
unsigned int f = 0; //4バイト、32ビット | |
printFlag( f ); | |
ON_FLAG( f, FLAG_B ); | |
printFlag( f ); | |
ON_FLAG( f, FLAG_A | FLAG_C ); | |
printFlag( f ); | |
printf( "%d\n", GET_FLAG( f, FLAG_C ) ); | |
OFF_FLAG( f, FLAG_A ); | |
printf( "%d\n", GET_FLAG( f, FLAG_A ) ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment