algorithm - Bitwise and in place of modulus operator -


We know that for example the modulo of power of two can be expressed in this way:

< Pre> X% 2 Strong N == X & amp; (2nd Force n - 1). Example:
  x% 2 == x & amp; nbsp; 1 x% 4 == X & amp; 3 x% 8 == X & amp; 7  

What about the general non-power of two numbers?

We say:

x% 7 ==?

First of all, it is not true that

  x % 2 == x; 1  

Simple counting: x = -1 . In many languages ​​including Java, -1% 2 == -1 . This is, % is not necessarily a traditional mathematical definition. For example module Java calls it "the rest operator".

Regarding bitwise optimization, only the modulo power of the two can be "easily" in the BitWord arithmetics. Generally, it can be easily done with the B representation based on the base b .

In base 10, for example, only the least important k for non-negative N , n mod 10 ^ k is taking points.

Reference


Comments