Skip to content

Instantly share code, notes, and snippets.

@danielkraic
Created December 12, 2017 16:23
Show Gist options
  • Save danielkraic/8509d8c35a625244fce96f8355b75761 to your computer and use it in GitHub Desktop.
Save danielkraic/8509d8c35a625244fce96f8355b75761 to your computer and use it in GitHub Desktop.
bits operartions
short val = 12;
// check if bit is set:
if (val & 0b0010) return true;
// ensure bit is 1:
val = val | 0b0010;
// ensure bit is o:
val = val & ~0b0010;
// toogle bit:
val = val ^ 0b0010;
int numOfSetBits(int val) {
if (val == 0) return 0;
return (val & 01) ? 1 + numOfSetBits(val >> 1) : numOfSetBits(val >> 1);
}
bool isEven(int val) {
return (val & 0x01 == 1) ? false : true;
}
bool isPowerOfTwo(int val) {
return ((val & (val-1)) == 0) ? true : false;
// return ((val & (-val)) == val) ? true : false;
}
void swap(int *a, int *b) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment