Skip to content

Instantly share code, notes, and snippets.

@skrajup
Forked from Coding-Enthusiast/BitwiseOperations.md
Created August 8, 2022 13:07
Show Gist options
  • Select an option

  • Save skrajup/7f79d54c63d6c36411ce8fe1cee0b524 to your computer and use it in GitHub Desktop.

Select an option

Save skrajup/7f79d54c63d6c36411ce8fe1cee0b524 to your computer and use it in GitHub Desktop.
Bitwise operations cheat sheet

AND (&)

100  
101  
---  
100  

OR (|)

100  
101  
---  
101  

XOR (^)

100  
101  
---  
001  

NOT (~)

101 
---  
010  

Shift (>>) and (<<)

0001_0111 >> 3 = 0000_0010
0001_0111 << 3 = 1011_1000

Arithmetic

Multiply x by 2k

x << k
Example: 5 * 8 = 5 << 3

Divide x by 2k

x >> k
Example: 20 / 16 = 20 >> 4

Mod by 2k

x & (2k-1)

Example: 20 % 16 = 20 & 15

Is x power of 2?

(x != 0) && (x & (x - 1)) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment