The
bit shifts are sometimes considered bitwise operations, because they treat a value as a series of bits rather than as a numerical quantity. In these operations, the digits are moved, or
shifted, to the left or right.
Registers in a computer processor have a fixed width, so some bits will be "shifted out" of the register at one end, while the same number of bits are "shifted in" from the other end; the differences between bit shift operators lie in how they determine the values of the shifted-in bits.
Bit addressing If the width of the register (frequently 32 or even 64) is larger than the number of bits (usually 8) of the smallest addressable unit, frequently called byte, the shift operations induce an addressing scheme from the bytes to the bits. Thereby the orientations "left" and "right" are taken from the standard writing of numbers in a
place-value notation, such that a left shift increases and a right shift decreases the value of the number ― if the left digits are read first, this makes up a
big-endian orientation. Disregarding the boundary effects at both ends of the register, arithmetic and logical shift operations behave the same, and a shift by 8 bit positions transports the bit pattern by 1 byte position in the following way: :
Arithmetic shift In an
arithmetic shift (sticky shift), the bits that are shifted out of either end are discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right arithmetic shift, the
sign bit (the MSB in two's complement) is shifted in on the left, thus preserving the sign of the operand. This example uses an 8-bit register, interpreted as two's complement: 00010111 (decimal +23) LEFT-SHIFT = 0010111
0 (decimal +46) 10010111 (decimal −105) RIGHT-SHIFT =
11001011 (decimal −53) In the first case, the leftmost digit was shifted past the end of the register, and a new 0 was shifted into the rightmost position. In the second case, the rightmost 1 was shifted out (perhaps into the
carry flag), and a new 1 was copied into the leftmost position, preserving the sign of the number. Multiple shifts are sometimes shortened to a single shift by some number of digits. For example: 00010111 (decimal +23) LEFT-SHIFT-BY-TWO = 010111
00 (decimal +92) A left arithmetic shift by
n is equivalent to multiplying by 2
n (provided the value does not
overflow), while a right arithmetic shift by
n of a
two's complement value is equivalent to taking the
floor of division by 2
n. If the binary number is treated as
ones' complement, then the same right-shift operation results in division by 2
n and
rounding toward zero.
Logical shift In a
logical shift (zero fill shift), zeros are shifted in to replace the discarded bits. Therefore, the logical and arithmetic left-shifts are exactly the same. However, as the logical right-shift inserts value 0 bits into the most significant bit, instead of copying the sign bit, it is ideal for unsigned binary numbers, while the arithmetic right-shift is ideal for signed
two's complement binary numbers.
Circular shift Another form of shift is the
circular shift,
bitwise rotation or
bit rotation.
Rotate In this operation, sometimes called
rotate no carry, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted into the right during a left-shift is whatever value was shifted out on the left, and vice versa for a right-shift operation. This is useful if it is necessary to retain all the existing bits, and is frequently used in digital
cryptography.
Rotate through carry Rotate through carry is a variant of the rotate operation, where the bit that is shifted in (on either end) is the old value of the carry flag, and the bit that is shifted out (on the other end) becomes the new value of the carry flag. A single
rotate through carry can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand. For example, if the carry flag contains 0, then x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is a logical right-shift, and if the carry flag contains a copy of the sign bit, then x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is an arithmetic right-shift. For this reason, some microcontrollers such as low end
PICs just have
rotate and
rotate through carry, and don't bother with arithmetic or logical shift instructions. Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native
word size, because if a large number is stored in two registers, the bit that is shifted off one end of the first register must come in at the other end of the second. With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.
In high-level languages In C family of languages In C and C++ languages, the logical shift operators are "<<" for left shift and ">>" for right shift. The number of places to shift is given as the second argument to the operator. For example, x = y assigns x the result of shifting y to the left by two bits, which is equivalent to a multiplication by four. Shifts can result in implementation-defined behavior or
undefined behavior, so care must be taken when using them. The result of shifting by a bit count greater than or equal to the word's size is undefined behavior in C and C++. Right-shifting a negative value is implementation-defined and not recommended by good coding practice; the result of left-shifting a signed value is undefined if the result cannot be represented in the result type. In C#, the right-shift is an arithmetic shift when the first operand is an int or long. If the first operand is of type uint or ulong, the right-shift is a logical shift.
Circular shifts The C-family of languages lack a rotate operator (although C++20 provides std::rotl and std::rotr), but one can be synthesized from the shift operators. Care must be taken to ensure the statement is well formed to avoid
undefined behavior and
timing attacks in software with security requirements. For example, a naive implementation that left-rotates a 32-bit unsigned value x by n positions is simply uint32_t x = ..., n = ...; uint32_t y = (x > (32 - n)); However, a shift by 0 bits results in undefined behavior in the right-hand expression (x >> (32 - n)) because 32 - 0 is 32, and 32 is outside the range 0–31 inclusive. A second try might result in uint32_t x = ..., n = ...; uint32_t y = n ? (x > (32 - n)) : x; where the shift amount is tested to ensure that it does not introduce undefined behavior. However, the branch adds an additional code path and presents an opportunity for timing analysis and attack, which is often not acceptable in high-integrity software. uint32_t x = ..., n = ...; uint32_t y = (x > (-n & 31)); There are also compiler-specific
intrinsics implementing
circular shifts, like _rotl8, _rotl16, _rotr8, _rotr16 in Microsoft
Visual C++. Clang provides some rotate intrinsics for Microsoft compatibility that suffers the problems above. • The operators (left shift), >> (signed right shift), and >>> (unsigned right shift) are called the
shift operators. • The type of the shift expression is the promoted type of the left-hand operand. For example, aByte >>> 2 is equivalent to . • If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive. • If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x3f (0b111111).
Pascal In Pascal, as well as in all its dialects (such as
Object Pascal and
Standard Pascal), the logical left and right shift operators are "shl" and "shr", respectively. Even for signed integers, shr behaves like a logical shift, and does not copy the sign bit. The number of places to shift is given as the second argument. For example, the following assigns
x the result of shifting
y to the left by two bits: x := y shl 2; == Other ==