Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tomaslibal
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaslibal/1949a366d1e7e40385fe to your computer and use it in GitHub Desktop.
Save tomaslibal/1949a366d1e7e40385fe to your computer and use it in GitHub Desktop.
#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