Li Yin
1 min readOct 10, 2017

Operations

Bitwise operators

Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

For example, 2 is 10 in binary and 7 is 111.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Special case:

#if an integer is odd or even
i&1 == 1 #even
#the value divided by 2 or 4 or 8
i>>1 # or log4, log8
#multiply by 2,4 ,8
i<<1
#get bit i, 1<<i = 00001000
(num &(1<<i))!=0
#set bit i,
num | (1<<i)
#clear bit