Skip to content

Instantly share code, notes, and snippets.

@Polda18
Last active February 7, 2018 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Polda18/0d5f9484d5330bedc5125253b6673ec1 to your computer and use it in GitHub Desktop.
Save Polda18/0d5f9484d5330bedc5125253b6673ec1 to your computer and use it in GitHub Desktop.
Complete usage of integer flag bits: edit how you need
/*************************************************************************
* 32bit integer bitwise flags
* Complete definition - by Polda18
*************************************************************************
* You may rename flags as you wish, and use as many as you wish.
* If for some reason you need more than 32 bits, you can expand
* the definition by adding more 32 bits and a switch "L" like that:
* 0x0000000000000000L => That makes the literal long int
*************************************************************************/
#ifndef __FLAGBITS_H__
#define __FLAGBITS_H__
// Definition of integer bitwise flags
// Notice the 1, 2, 4 and 8 pattern in the hexadecimal format.
// Each digit is a representation of 4 bits.
// 1, 2, 4 and 8 are a single bit representations:
// 1 = 0001
// 2 = 0010
// 4 = 0100
// 8 = 1000
// Complete definition:
#define FLAG_BIT_00 0x00000001
#define FLAG_BIT_01 0x00000002
#define FLAG_BIT_02 0x00000004
#define FLAG_BIT_03 0x00000008
#define FLAG_BIT_04 0x00000010
#define FLAG_BIT_05 0x00000020
#define FLAG_BIT_06 0x00000040
#define FLAG_BIT_07 0x00000080
#define FLAG_BIT_08 0x00000100
#define FLAG_BIT_09 0x00000200
#define FLAG_BIT_10 0x00000400
#define FLAG_BIT_11 0x00000800
#define FLAG_BIT_12 0x00001000
#define FLAG_BIT_13 0x00002000
#define FLAG_BIT_14 0x00004000
#define FLAG_BIT_15 0x00008000
#define FLAG_BIT_16 0x00010000
#define FLAG_BIT_17 0x00020000
#define FLAG_BIT_18 0x00040000
#define FLAG_BIT_19 0x00080000
#define FLAG_BIT_20 0x00100000
#define FLAG_BIT_21 0x00200000
#define FLAG_BIT_22 0x00400000
#define FLAG_BIT_23 0x00800000
#define FLAG_BIT_24 0x01000000
#define FLAG_BIT_25 0x02000000
#define FLAG_BIT_26 0x04000000
#define FLAG_BIT_27 0x08000000
#define FLAG_BIT_28 0x10000000
#define FLAG_BIT_29 0x20000000
#define FLAG_BIT_30 0x40000000
#define FLAG_BIT_31 0x80000000
// To use flags, simply set a new single integer variable that uses these flags:
//int flags = 0;
// And then assign flags using bitwise OR operation:
//flags |= FLAG_BIT_11 | FLAG_BIT_20;
// An example of integer bitwise flags as may be really used:
#define SURFACE_HINT 0x00000001
#define SURFACE_LADDER 0x00000002
#define SURFACE_RAILWAY 0x00000004
#define SURFACE_ANVIL 0x00000008
#endif
@Polda18
Copy link
Author

Polda18 commented Feb 7, 2018

If you need more than 32 bits of flags, you'll need to define long int literals (denoted as 0x0000000000000000L), giving you access to up to 64 bits of flags. Then the flags variable will be defined as long int flags.

Remember you don't need to defined flags variable as unsigned int as hexadecimal denotation is the same for signed and unsigned integers. The same applies for long int.

This method is suitable for both C and C++ projects. It does not support namespace branching. If you want to define flags in namespaces, you have to create constant variables inside the namespace instead (const int FLAG_BIT = 0x00000000;).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment