0

Given integers n and m, how can I determine that n is a multiple of m using bitwise operators, without any fancy n % m == 0?

Joseph Kirtman
  • 349
  • 2
  • 10
  • 1
    It's quite easy if m is a power of 2. Otherwise it's quite challenging. – Paul R Nov 22 '18 at 16:24
  • I know that m is even – Joseph Kirtman Nov 22 '18 at 16:28
  • If `m` is a constant then it can be done with a multiplication and a bitwise rotate, but multiplication is hardly bitwise.. and if `m` is a variable it doesn't work out that well (it's still possible but requires a dozen extra multiplies) – harold Nov 22 '18 at 23:06
  • Possible duplicate of [Bitwise and in place of modulus operator](https://stackoverflow.com/questions/3072665/bitwise-and-in-place-of-modulus-operator) – Mukul Gupta Nov 23 '18 at 20:39
  • There is no easy way. You may want to refer to the referenced links in the answer https://stackoverflow.com/a/19760152/1112010. – Mukul Gupta Nov 23 '18 at 20:40

0 Answers0