Is it possible to rewrite modulo (2^n - 1) using bitwise and restricted operators

后端 未结 3 1341
-上瘾入骨i
-上瘾入骨i 2021-02-05 20:48

For unsigned int x, is it possible to calculate x % 255 (or 2^n - 1 in general) using only the following operators (plus no loop, branch or function call)?

!

3条回答
  •  灰色年华
    2021-02-05 21:27

    Just create an array with all the values (only either need 32 or 64 entries (i.e. 128 or 512 bytes). Then just do a look up.

提交回复
热议问题