Skip to content

Instantly share code, notes, and snippets.

@gszauer
Created June 6, 2013 00:39
Show Gist options
  • Save gszauer/5718480 to your computer and use it in GitHub Desktop.
Save gszauer/5718480 to your computer and use it in GitHub Desktop.
#define Bit(number) (1 << number)
#define BitValue(variable, bitmask) (variable & bitmask)
#define BitOn(variable, bitmask) (variable |= bitmask)
#define BitOff(variable, bitmask) (variable &= ~bitmask)
#define BitToggle(variable, bitmask) (variable ^ bitmask)
#define PowerOfTwo(integer) (!(integer - 1) & integer)
template <typename T>
void print_bits ( T val, std::ostream& out ) {
T n_bits = sizeof ( val ) * CHAR_BIT;
for ( unsigned i = 0; i < n_bits; ++i ) {
out<< !!( val & 1 );
val >>= 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment