Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find if a number is divisible by 8 - using bit shifting operators

I was recently asked during an interview, using just bit shift operators, write some code that would tell you if a number is divisible by 8, apparently the code is very short - does anyone have a clue?

Note: if you aren't required to use shifts, you'd test the low n bits for being all-zero with a mask like x&7 == 0, or more generally x & ((1UL << n) - 1) == 0. How can I tell if a number is a multiple of four using only the logic operator AND?

like image 479
godzilla Avatar asked Jun 24 '13 18:06

godzilla


People also ask

How do you know if a binary number is divisible by 8?

To test divisibility by 8, you look at the last three digits. You consider the hundred's digit, and then the last two digits. If the hundred's digit is even, then the last two digits must be divisible by 8. If the hundred's digit is odd, then the last two digits must be divisible by 4 but not 8.

How do you divide a number with a shift operator?

Division using the left shift operator. We know that each number can be represented as the sum of powers of 2 and also when we shift a number towards left by n bits it is multiplied by 2 power n . Thus, what we do is shift the divisor b towards the left and check if it is less than or equal to the dividend a .

How do you check if a number is divisible by 8 in Python?

Alternative Method: num = int (input (“Enter the number whose divisibility needs to be checked:”)) div = int (input (“Enter the number with which divisibility needs to be checked:”)) if num%div == 0: print (“The number is divisible.”) else: print (“The number is not divisible.”)


2 Answers

With any integer represented in binary the remainder of division by any power of two is simply the value of the bits of lower order so 0b11001110 divided by 0b1000 has remainder 0b110. So in order to check for divisibility by 8 you need to check if the three low order bits are all zero:

if (((x >> 3) << 3) == x)
  divisibleBy8 = true;

Right shifting clears the bottom three bits before the left shift restores the magnitude and then compare to the original number.

As others have pointed out, if you know the bit width of the integer you can do this

if (!(x<<29))
  divisibleby8 = true;

Replace that 29 by 61 for 64-bit integers, etc. Apparently in Java you can do this:

if ((x << -3) != 0)
  divisibleby8 = true;

Because negative shifts such as -3 are interpreted as bit_width - 3 and it will work with both 32- and 64-bit integers.

(You don't need all the brackets, I've included for clarity)

Just for completeness

These are all pretty bad ways to test for divisibility by 8. Doing if !(x & 7) is clearer and almost certainly as fast or faster.

like image 141
Jack Aidley Avatar answered Mar 19 '23 07:03

Jack Aidley


int num;

if(!(num & 7)) {
     // num divisible by 8
}

or

if(! (num << 29) ) { // assuming num is 32 bits
    // num divisible by 8
}
like image 27
Sergey L. Avatar answered Mar 19 '23 06:03

Sergey L.