How do computers find modulus?

前端 未结 6 1755
时光说笑
时光说笑 2021-01-04 14:15

Is there some cool algorithm with bit wise operations?

6条回答
  •  清歌不尽
    2021-01-04 14:43

    Apart from the obvious method using DIV and IDIV (for x86) as mentioned above, the result of any number modulo'd by a power of two can be calculated by taking the bitwise and: x mod y where y is pow2 is the same as x AND (y - 1). Most compilers perform this when possible, as division is far more expensive than bitwise ops

提交回复
热议问题