Using the calculator
Type a number, a shift and how far: `1 << 4`, `-16 >> 2`, `-16 >>> 2`, `81 ROL 1`. Set Bits to the width of the register you're working with, and Numbers are in to decimal, hex or binary. The working shows every bit before and after, so you can see what moved, what came in and what fell off.
Negative numbers are allowed, and are shifted as their two's-complement bit pattern. The answer shows both the unsigned value of the result and, if the top bit is set, what it means as a signed number.
Left shift
`<<` moves every bit towards the top and fills the bottom with zeros. Each place multiplies by 2, so `1 << 4` is 16. In a fixed-width register, bits pushed past the top are lost: `F0 << 4` in 8 bits is 0, and the working says what the answer would have been with room to spare.
Arithmetic and logical right shifts
Shifting right moves bits towards the bottom, and the only question is what comes in at the top. A logical shift, `>>>`, brings in zeros. An arithmetic shift, `>>`, brings in copies of the top bit, the sign bit, so a negative number stays negative. In 8 bits, −16 is 1111 0000. Arithmetic shift right by 2 gives 1111 1100, which is −4. Logical shift right by 2 gives 0011 1100, which is 60.
An arithmetic right shift divides by a power of 2, but it rounds down, towards minus infinity. Integer division in most languages rounds towards zero. So −1 >> 1 is still −1, while −1 / 2 is 0.
Rotations, ROL and ROR, are shifts where nothing is lost: the bits that fall off one end come back in at the other.
What >> means depends on the language
C never said what `>>` does to a negative signed number. The ISO C standard leaves it to each compiler, and C++ did the same until C++20, which finally defined it as an arithmetic shift. Java and JavaScript settled it by having two operators, `>>` for arithmetic and `>>>` for logical, because neither has unsigned integer types to say which shift is meant.
Questions
What's the difference between >> and >>>?
>> is an arithmetic shift, which copies the sign bit into the top. >>> is a logical shift, which fills the top with zeros. They only differ for numbers whose top bit is 1.
Is shifting left the same as multiplying by 2?
Yes, as long as nothing falls off the top of the register. Once bits are lost, the answer wraps round.
Why is -1 >> 1 still -1?
-1 is all 1s in two's complement. Shifting right and copying the sign bit back in leaves it all 1s.
What width should I pick?
The size of the variable you're modelling: 8 for a byte, 32 for an int in most languages, 64 for a long.
Sources
Added . What's new






