Skip to content
Surf Wiki
Save to docs
general/binary-arithmetic

From Surf Wiki (app.surf) — the open knowledge base

Logical shift

Bit-level computer operation


Bit-level computer operation

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.

Language or processorLeftRight
Ada
Batch, C, [C++](c), Go, Swift (unsigned types only);
Standard ML, Verilog, PHP, Python, Rust (unsigned types only){{mono
D, Java, JavaScript, Julia{{mono
F# (unsigned types only){{mono
Fortran
OCaml
Object Pascal, Delphi, x86 assembly, Kotlin, Powershell
VHDL, MIPS, RISC-V
PowerPC

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 , but the arithmetic right shift operator is . (Java has only one left shift operator ({{mono|

The programming languages C, C++, and Go, however, have only one right shift operator, . 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, 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.

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

Example

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

Shift left yields: 0010 1110 (decimal 46)Shift right yields: 0000 1011 (decimal 11)

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

References

zh:位操作#逻辑移位

References

  1. "The Package Interfaces".
  2. "Set - Environment Variable - Windows CMD - SS64.com".
  3. "BitwiseOperators - Python Wiki".
  4. "Shl in std::ops - Rust".
  5. "Operator Expressions: Arithmetic and Logical Binary Operators".
  6. "PowerPC Instruction Set: slw".
  7. "x86 Instruction Set Reference".
  8. "Opcodes.Shl Field". Microsoft.
  9. "LLVM Language Reference Manual - shl Instruction". LLVM Project.
  10. "<< Operator (C# Reference)". Microsoft.
Info: Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about Logical shift — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report