Movatterモバイル変換


[0]ホーム

URL:


Wikipedia

Logical shift

"shift left" redirects here. For the software testing practice, seeshift-left testing.
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Logical shift" – news ·newspapers ·books ·scholar ·JSTOR
(April 2016) (Learn how and when to remove this message)

Incomputer science, alogical shift is abitwise operation that shifts all the bits of its operand. The two base variants are thelogical left shift and thelogical right shift. This is further modulated by the number of bit positions a given value shall be shifted, such asshift left by 1 orshift right by n. Unlike anarithmetic shift, a logical shift does not preserve a number's sign bit or distinguish a number'sexponent from itssignificand (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 acircular shift).

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

Logical shift operators in various programming languages and processors
Language or processorLeftRight
Ada[1]Shift_LeftShift_Right
Batch,[2]C,C++,Go,Swift (unsigned types only);
Standard ML,Verilog,PHP,Python,[3]Rust[4] (unsigned types only[5])
<< >>
D,Java,JavaScript,Julia << >>>
F# (unsigned types only)<<<>>>
FortranLSHIFTRSHIFT
OCamllsllsr
Object Pascal,Delphi,x86 assembly,Kotlin,Powershellshlshr
VHDL,MIPS,RISC-Vsllsrl
PowerPCslwsrw

Logical shifts can be useful as efficient ways to perform multiplication or division of unsignedintegers by powers of two. Shifting left byn bits on a signed or unsigned binary number has the effect of multiplying it by 2n. Shifting right byn bits on anunsigned 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 differentoperators for them. For example, inJava andJavaScript, the logical right shift operator is>>>, but the arithmetic right shift operator is>>. (Java has only one left shift operator (<<), because left shift via logic and arithmetic have the same effect.)

Theprogramming languagesC,C++, andGo, 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 inPowerPC chooses the more-intuitive behavior where shifting by the bit width or above gives zero,[6] whereas SHL inx86 chooses to mask the shift amount to the lower bitsto 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 andLLVM, also leave shifting by the bit width and aboveunspecified (.NET)[8] orundefined (LLVM).[9] Others choose to specify the behavior of their most common target platforms, such asC# which specifies the x86 behavior.[10]

Example

edit

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

Shift left yields: 0010 1110 (decimal 46)
 
Logical left shift one bit
Shift right yields: 0000 1011 (decimal 11)
 
Logical right shift one bit

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

References

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

[8]ページ先頭

©2009-2025 Movatter.jp