Bit shifts offer a method of quickly performing certain mathematical operations, such as multiplication and division, on binary numbers. This type of low-level math significantly speeds execution of programs, since it avoids high-level math operations. Use a bit shift right to divide an integer by a power of two without the need for a calculator or a mathematical mind.
Express the dividend (the number that you're dividing) in binary form. The resulting number consists of a series of ones and zeros. Let the number of bits be B.
Determine the power of two that represents the divisor. (For example, 4 is 2 to the second power and 16 is 2 to the fourth power.) Let N represent this number.
Start a new binary number, writing from left to right, with N zeros. After the zeros, copy the bits from the dividend until the new number has the same number of bits as the dividend. If N is greater than or equal to the number of bits in the dividend, simply write B zeros.
Ignore the last N bits of the dividend, since the division (or shift) eliminates these bits. The result is a binary number with the same number of bits as the dividend.
Convert the result back to the original form. If the original dividend was in base 10, for example, then convert back to base 10.
Tips & Warnings
- Integer division using a bit shift only works when the divisor is a power of two, such as 2, 4, 8, 16 and 32.
- Computers do not convert between base number systems; they operate exclusively in binary. Conversion between base number systems is for the benefit of computer users.
- The kind of simple division described here involves only integers, so the result is an integer with any remainder discarded.