Faster modulus in C/C#?

后端 未结 7 1186
情话喂你
情话喂你 2020-12-25 13:07

Is there a trick for creating a faster integer modulus than the standard % operator for particular bases?

For my program, I\'d be looking for around 1000-4000 (e.g.

7条回答
  •  我在风中等你
    2020-12-25 13:32

    You could zero out the high order bits i.e.

    x = 11 = 1011
    x % 4 = 3 = 0011

    so for x % 4 you could just take the last 2 bits - I'm not sure what would happen if negative numbers were used though

提交回复
热议问题