Bitwise modulus computation
Given two numbers a and b where b is of form 2k where k is unknown.What would be t开发者_Go百科he efficient way of computing a%b using bitwise operator.
a AND (b-1) == a%b (when b is 2^k)
ex. a = 11 (1011b), b = 4 (0100b)
11 / 4 = 2 R3
11 % 4 == 11 AND (4-1)
11 (1011b) AND 3 (0011b) == 3 (0011b)
精彩评论