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

后端 未结 3 1332
-上瘾入骨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:40

    Sure. Just get out one of your old computer architecture textbooks and refresh your memory on boolean algebra. A CPU's ALU does it with ANDs and ORs; you can, too.

    But why?

    An academic exercise? Homework? Curiousity?

提交回复
热议问题