What a bit shift does to a number

A computer stores integers as rows of binary digits, or bits. A shift moves every bit one or more places along that row. For example, in an eight-bit illustration, `00001101` represents 13. Shift it left once and it becomes `00011010`, or 26: each bit has moved into the place worth twice as much.

Shift right once and `00001101` becomes `00000110`, or 6. The final 1 has fallen off the end. For nonnegative integers, a right shift therefore usually acts like division by two with any fractional remainder discarded.

Why the result is not always simple arithmetic

The familiar rule—left means multiply by two, right means divide by two—depends on the number’s representation and the language’s rules. Bits shifted past the edge are lost, so a left shift can discard high-order bits; in a fixed-width value, the result may no longer equal ordinary multiplication. Signed integers add another wrinkle: a right shift may copy the leading sign bit, or fill the newly opened space with zeros, depending on the operation and language.

Why programmers use shifts

Shifts are useful when a program needs to inspect or arrange individual flags, such as settings packed together in one integer, or to move pieces of a binary value into position. They can also express multiplication or division by powers of two when the type and range make that safe. The key is to think of a shift as moving places in a finite row of bits—not as arithmetic that can never lose information.