开发者

Bitwise modulus computation

开发者 https://www.devze.com 2023-03-28 17:29 出处:网络
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)

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消