Wednesday 28 June 2017

multiply and divide using only bit shifting

X * 2 = 1 bit shift left = X<<1 span="">
X / 2 = 1 bit shift right = X>>1
X * 3 = shift left 1 bit and then add X



x * 14 == x * 16 - x * 2 == (x << 4) - (x << 1)
x * 12 == x * 8 + x * 4 == (x << 3) + (x << 2) 


To divide a number by a non-power of two, It may not possible in easy way and it need  to implement some low-level logic, use other binary operations and use some form of iteration.


No comments:

Post a Comment