Logical shift

From Wikipedia, the free encyclopedia

Template:Short description Lua error in package.lua at line 80: module 'Module:Hatnote list' not found.

Page Module:Message box/ambox.css has no content.

In computer science, a logical shift is a bitwise operation that shifts all the bits of its operand. The two base variants are the logical left shift and the logical right shift. This is further modulated by the number of bit positions a given value shall be shifted, such as shift left by 1 or shift right by n. Unlike an arithmetic shift, a logical shift does not preserve a number's sign bit or distinguish a number's exponent from its significand (mantissa); every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled, usually with zeros, and possibly ones (contrast with a circular shift).

A logical shift is often used when its operand is being treated as a sequence of bits instead of as a number.

Logical shift operators in various programming languages and processors
Language or processor Left Right
Ada[1] Page Template:Mono/styles.css has no content.Shift_Left Page Template:Mono/styles.css has no content.Shift_Right
Batch,[2] C, C++, Go, Swift (unsigned types only);
Standard ML, Verilog, PHP, Python,[3] Rust[4] (unsigned types only[5])
Page Template:Mono/styles.css has no content. << Page Template:Mono/styles.css has no content. >>
D, Java, JavaScript, Julia Page Template:Mono/styles.css has no content. << Page Template:Mono/styles.css has no content. >>>
F# (unsigned types only) Page Template:Mono/styles.css has no content.<<< Page Template:Mono/styles.css has no content.>>>
Fortran Page Template:Mono/styles.css has no content.LSHIFT Page Template:Mono/styles.css has no content.RSHIFT
OCaml Page Template:Mono/styles.css has no content.lsl Page Template:Mono/styles.css has no content.lsr
Object Pascal, Delphi, x86 assembly, Kotlin, Powershell Page Template:Mono/styles.css has no content.shl Page Template:Mono/styles.css has no content.shr
VHDL, MIPS, RISC-V Page Template:Mono/styles.css has no content.sll Page Template:Mono/styles.css has no content.srl
PowerPC Page Template:Mono/styles.css has no content.slw Page Template:Mono/styles.css has no content.srw

Logical shifts can be useful as efficient ways to perform multiplication or division of unsigned integers by powers of two. Shifting left by n bits on a signed or unsigned binary number has the effect of multiplying it by 2n. Shifting right by n bits on an unsigned binary number has the effect of dividing it by 2n (rounding towards 0).

Logical right shift differs from arithmetic right shift. Thus, many languages have different operators for them. For example, in Java and JavaScript, the logical right shift operator is Page Template:Mono/styles.css has no content.>>>, but the arithmetic right shift operator is Page Template:Mono/styles.css has no content.>>. (Java has only one left shift operator (Page Template:Mono/styles.css has no content.<<), because left shift via logic and arithmetic have the same effect.)

The programming languages C, C++, and Go, however, have only one right shift operator, Page Template:Mono/styles.css has no content.>>. Most C and C++ implementations, and Go, choose which right shift to perform depending on the type of integer being shifted: signed integers are shifted using the arithmetic shift, and unsigned integers are shifted using the logical shift. In particular, C++ uses its logical shift operators as part of the syntax of its input and output functions, called "cin" and "cout" respectively.

All currently relevant C standards (ISO/IEC 9899:1999 to 2011) leave a definition gap for cases where the number of shifts is equal to or bigger than the number of bits in the operands in a way that the result is undefined. This helps allow C compilers to emit efficient code for various platforms by allowing direct use of the native shift instructions which have differing behavior. For example, shift-left-word in PowerPC chooses the more-intuitive behavior where shifting by the bit width or above gives zero,[6] whereas SHL in x86 chooses to mask the shift amount to the lower bits to reduce the maximum execution time of the instructions, and as such a shift by the bit width doesn't change the value.[7]

Some languages, such as the .NET Framework and LLVM, also leave shifting by the bit width and above unspecified (.NET)[8] or undefined (LLVM).[9] Others choose to specify the behavior of their most common target platforms, such as C# which specifies the x86 behavior.[10]

Example

If the bit sequence 0001 0111 (decimal 23) is logically shifted by one bit position, then:

Shift left yields: 0010 1110 (decimal 46)
File:Rotate left logically.svg
Logical left shift one bit
Shift right yields: 0000 1011 (decimal 11)
File:Rotate right logically.svg
Logical right shift one bit

Note: MSB = Most Significant Bit, LSB = Least Significant Bit

References

Page Template:Reflist/styles.css has no content.

  1. ^ Page Module:Citation/CS1/styles.css has no content."The Package Interfaces". www.adaic.org.
  2. ^ Page Module:Citation/CS1/styles.css has no content."Set - Environment Variable - Windows CMD - SS64.com". ss64.com.
  3. ^ Page Module:Citation/CS1/styles.css has no content."BitwiseOperators - Python Wiki". wiki.python.org. Retrieved 2018-01-24.
  4. ^ Page Module:Citation/CS1/styles.css has no content."Shl in std::ops - Rust". doc.rust-lang.org. Retrieved 2022-01-17.
  5. ^ Page Module:Citation/CS1/styles.css has no content."Operator Expressions: Arithmetic and Logical Binary Operators". doc.rust-lang.org. Retrieved 2022-11-13.
  6. ^ Page Module:Citation/CS1/styles.css has no content."PowerPC Instruction Set: slw". pds.twi.tudelft.nl. Archived from the original on 4 September 2014. Retrieved 9 April 2016.
  7. ^ Page Module:Citation/CS1/styles.css has no content."x86 Instruction Set Reference". x86.renejeschke.de. Archived from the original on 19 February 2018. Retrieved 9 April 2016.
  8. ^ Page Module:Citation/CS1/styles.css has no content."Opcodes.Shl Field". msdn.microsoft.com. Microsoft. Retrieved 9 April 2016.
  9. ^ Page Module:Citation/CS1/styles.css has no content."LLVM Language Reference Manual - shl Instruction". llvm.org. LLVM Project. Retrieved 9 April 2016.
  10. ^ Page Module:Citation/CS1/styles.css has no content."<< Operator (C# Reference)". msdn.microsoft.com. Microsoft. Retrieved 9 April 2016.