Logical shift

In a logical shift ( engl. logical shift ) is understood in the computer science a bitwise operator, which shifts all the bits of the operand. The logical as opposed to arithmetic shift receives neither the sign nor distinguishes them between the exponent and the mantissa of a number. Each bit of the operand is simply moved to a given position number, and the resulting bit positions with zeros filled in the rule.

A logical shift is often used when the address is considered as a sequence of bits, and not as a number.

Logical shifts can be a useful and efficient way to perform multiplication or division of unsigned integers (whole numbers) to the second power. A left shift by n bits in an unsigned or signed binary number has the effect of multiplying it by 2n. Move to the right by n bits in an unsigned binary number is equivalent to dividing by 2 n ( and subsequent rounding ).

Because differ arithmetic shifts and logical shifts to the right, many programming languages ​​have different operators for them. So the arithmetic shift operator in Java and JavaScript, for example, >>; whereas the logical right shift is. >>> Java alone has only a left shift operator (<< ) because arithmetic and logical shift have the same effect.

The C language again has only one right shift operator >>. Many C compilers decide which right shift is meant, then what Integer data type is to be moved; often unsigned integers are shifted by arithmetic, and unsigned by the logical shift.

Example

The following bit sequence to be shifted logically by 1 bit: 0001 0111 (see pictures)

  • Result ... to the left: 0010 1110
  • Results ... to right: 0000 1011
  • Computer arithmetic
  • Programming language element
527373
de