Last active
August 29, 2015 14:00
-
-
Save tomaslibal/1949a366d1e7e40385fe to your computer and use it in GitHub Desktop.
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> | |
const short FLAG_1 = 1; | |
const short FLAG_2 = 1 << 1; | |
const short FLAG_3 = 1 << 2; | |
const short FLAG_4 = 1 << 3; | |
//int settings = FLAG_1|FLAG_3|FLAG_4; | |
int settings = 0; | |
void set_flag(int flag); | |
void unset_flag(int flag); | |
int is_flag_set(int flag); | |
int is_flag_set_bool(int flag); | |
int main() | |
{ | |
printf("%d %d %d %d\n", FLAG_1, FLAG_2, FLAG_3, FLAG_4); | |
printf("FLAG_2 set = %d\n", is_flag_set(FLAG_2)); | |
set_flag(FLAG_2); | |
set_flag(FLAG_3); | |
printf("FLAG_2 set = %d\n", is_flag_set(FLAG_2)); | |
printf("FLAG_3 set = %d\n", is_flag_set(FLAG_3)); | |
printf("FLAG_4 set = %d [0 = no, 1 = yes]\n", is_flag_set_bool(FLAG_4)); | |
unset_flag(FLAG_2); | |
printf("FLAG_2 set = %d [0 = no, 1 = yes]\n", is_flag_set_bool(FLAG_2)); | |
set_flag(FLAG_4); | |
printf("FLAG_4 set = %d [0 = no, 1 = yes]\n", is_flag_set_bool(FLAG_4)); | |
return 0; | |
} | |
void set_flag(int flag) { | |
settings |= flag; | |
} | |
void unset_flag(int flag) { | |
settings &= ~flag; | |
} | |
int is_flag_set(int flag) { | |
return settings & flag; | |
} | |
int is_flag_set_bool(int flag) { | |
return (settings & flag) > 0 ? 1 : 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment