Skip to content

Instantly share code, notes, and snippets.

@Trevahok
Last active July 30, 2020 05:31
Show Gist options
  • Save Trevahok/9b49ec98cff97b4aa6c090407fb15a2a to your computer and use it in GitHub Desktop.
Save Trevahok/9b49ec98cff97b4aa6c090407fb15a2a to your computer and use it in GitHub Desktop.
Bitwise Tricks with CPP
Operation Code
To remove last set bit x & ( x - 1 )
To double x <<= 1
To halve x >>= 1
To lowercase x | ‘ ‘
To togglecase x ^ ‘ ‘
Is power of two? x && !(x & (x-1) )
log base 2 ( brian kern algorithm) int res = 0; while (x >>= 1) res++; return res;
multiply by 7 ( (x<<3) - x )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment