Bit Shift Calculator

Free bit shift calculator: left shift, right shift, arithmetic and logical shifts, and rotate, with a before-and-after bit view and results in binary, octal, decimal and hex across 4, 8, 16 and 32-bit registers.

https://hexacalculator.com/calculators/other/computers/bit-shift-calculator

Other

Computers

Bit Shift Calculator

Free bit shift calculator: left shift, right shift, arithmetic and logical shifts, and rotate, with a before-and-after bit view and results in binary, octal, decimal and hex across 4, 8, 16 and 32-bit registers.

Bit Shift Calculator

Bit shift

A left shift moves every bit toward the high end and multiplies the value by a power of two. A right shift moves bits the other way and divides.

Logical shifts pull in zeros. Arithmetic right shifts copy the sign bit so negative numbers stay negative. Rotate feeds the bit that falls off one end back in at the other, so nothing is lost. On a left shift, logical and arithmetic are identical.

How many bit positions the register holds. Width sets where the sign bit lives, what wraps on a rotate, and which high bits overflow on a left shift.

Number in the register
Number in binary
Result in octal
Shift factor (2 to the power of the shift)

How the shift works

Before: 00000101 (binary)

After: 00010100 (binary)

Result in hexadecimal: 14

Shifting left by this many bits multiplies the number by the shift factor shown above, as long as no 1 bits overflow the top of the register.

Every bit position of the register, before and after the shift

Bit (2n)

Before

After

2^700
2^600
2^500
2^401
2^300
2^211
2^100
2^010
Loading calculator…

Bit shifts move the ones and zeros of a binary number to the left or right by a specified amount. This is one of the fastest operations that a processor can perform, and it's also a way to efficiently do multiplication and division by powers of two.

If you enter a value and select the direction and shift mode this tool will show the result in decimal, binary, octal and hexadecimal representation and visualize each bit of the register in a comparison view.

Left shift and right shift

A left shift moves all bits towards the higher positions and fills the freed position on the right with a value of 0. With each bit shifted, the value doubles, so that a left shift by n bits is equivalent to multiplying by 2 raised to the power of n.

52=20(000001012000101002,  5×4=20)5 \ll 2 = 20 \qquad (00000101_2 \rightarrow 00010100_2,\; 5 \times 4 = 20)

A right shift moves all bits towards the lower order positions with the contents pushed out at the bottom end being discarded. With each bit shifted, the value is halved and truncated.

202=5(000101002000001012,  20÷4=5)20 \gg 2 = 5 \qquad (00010100_2 \rightarrow 00000101_2,\; 20 \div 4 = 5)

Since bit shifts directly correspond to multiplication or division by powers of two, the compiler will often quietly convert expressions like "multiply by eight" into a left shift by three bits.

Logical shift, arithmetic shift, rotate shift.

The difference between the three types of shifts is only how the bits freed up are treated, which is most important in a right shift.

Logical shift always inserts a zero bit. This is the correct choice for unsigned numbers, where each bit is simply part of the value.

Arithmetic right shift copies the sign bit (the leftmost bit) to the vacant position; thus negative numbers remain negative, which is similar to division by two with truncation towards negative infinity.

82=2(111110002111111102, 8-bit, sign kept)-8 \gg 2 = -2 \qquad (11111000_2 \rightarrow 11111110_2,\ \text{8-bit, sign kept})

A rotation (also called a circular shift) is different, because the bits that are pushed off one end come back in at the other end, so no bits are lost and the number of ones stays the same. Circular shifts are often used in hash functions and cryptography.

101100012  ROL  1=011000112(rotate left by 1)10110001_2 \;\text{ROL}\; 1 = 01100011_2 \qquad \text{(rotate left by 1)}

For a left shift, the result of a logical and arithmetic shift is identical because both insert zeros into the lower bits.

Register width and overflow

In actual hardware, registers are fixed size. Nibbles are 4 bits, bytes are 8 bits, and 16 or 32 bit registers are used all over the processor. The chosen bit width determines three things: where the sign bit is located, where a cyclic shift "wraps around", and which bits are considered to be overflow.

When you shift 1 left past the most significant bit of a register, those bits are lost forever. Since the shift no longer corresponds to simple multiplication in this case, this calculator shows it. To get the bits back, the width of the register must be increased.

Width

Unsigned range

Signed range (two's complement)

4-bit

0 to 15

-8 to 7

8-bit

0 to 255

-128 to 127

16-bit

0 to 65,535

-32,768 to 32,767

32-bit

0 to 4,294,967,295

-2,147,483,648 to 2,147,483,647

Negative numbers and two's complement

Fixed-width registers have no minus sign; negative numbers are stored in two's complement form. First the absolute value is represented in binary, then all bits are inverted and finally one is added. In an eight-bit register, the number minus Eight would be represented as 11111000.

If a negative number is entered, the calculator reads the corresponding bit pattern in two's complement into the register and outputs both the unsigned and signed value of the result. So it can be interpreted both ways.

Applications of bit shifts

Shift and rotate operations are used throughout low-level code to multiply or divide numbers by powers of two, pack multiple small values into a single integer, and extract color channels from packed pixels.

They are used to create and read bitmasks for hardware registers and function flags, to iterate through memory in steps corresponding to the size of an element, and to mix bits in checksums, hash functions, and encryption.

How to use this calculator:

Enter the value to shift and the number of bits to shift by. Select a left or right shift and select the shift mode. A logical shift is suitable for normal operations with unsigned numbers, while an arithmetic shift is used to preserve the sign bit of negative numbers, and a rotate is used to move bits between ends.

Set the register width appropriate for storing your data. The result will be shown in decimal, binary, octal and hexadecimal formats, with a bit representation showing the state of the register before and after the shift.

Frequently asked questions

Is a left shift the same as multiplying by two?

Yes. Each left shift doubles the value, so an n-bit left shift is equivalent to multiplying by 2^n. Shifting 5 two bits to the left gives you 20, which is the same as 5 times 4. This relationship holds true as long as the number of bits (1) does not exceed the most significant bit of the register.

What is the difference between logical right shift and arithmetic right shift?

A logical right shift fills the upper bits with zeroes making it suitable for unsigned numbers. An arithmetic right shift copies the sign bit, thus negative numbers remain negative. If −8 (in an 8-bit register) is arithmetically shifted to the right by 2 bits, this results in −2 since the leading 1 will be preserved.

What is the function of a rotation shift or cyclic shift?

A cyclic shift is a bit shift where the bits that are "shifted out" at one end reappear at the other end, so no bits are lost and the number of 1s remains unchanged. Rotating 10110001 by one bit to the left gives 01100011. Cyclic shifts are often used in hash functions and cryptography because every bit must be preserved.

How does register width affect the result?

The bit width determines the number of bit positions, the position of the sign bit and where a cyclic shift repeats itself. It also affects overflow behavior. If you shift the value 1 past the most significant bit of the register, that bit is lost forever. Even the same left shift fits in a 16-bit register while causing an overflow in a 4-bit register.

Can you shift negative numbers?

Yes, it is possible. Negative numbers are loaded into the register as a bit pattern in two's complement and then shifted. The calculator outputs both unsigned and signed values so you can read either the original bit pattern or the corresponding signed value.

Related calculators

Disclaimer: This calculator is provided for general informational and educational purposes only. Our calculators are under active development, and results may be inaccurate, incomplete, or unsuitable for your situation. Always verify the figures independently and seek advice from a qualified professional before relying on them. We make no warranties and accept no liability for any loss or decision arising from use of this tool.

References

  1. MDN: Bitwise operators (shift operators)

    Reference for the left shift, right shift, and unsigned right shift operators.

  2. Wikipedia: Bitwise operation (bit shifts)

    Definitions of logical and arithmetic shifts and how they relate to multiply and divide.

  3. Wikipedia: Circular shift

    How rotate left and rotate right move bits around a fixed-width register.

  4. Wikipedia: Two's complement

    How fixed-width registers represent signed numbers and their sign bit.