ArticleslgStudy

science

Logical shift

Logical shift is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Logical shift rather than just read about it. In short: 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.

Logical shift — main illustration
Logical shift — illustration

Key takeaways

  • Logical shift belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Logical shift to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Logical shift from memory before moving on to harder problems.

Reference excerpt

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 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 (<<), 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, >>. 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:

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

References

Illustrations

Logical shift: Logical right shift one bit
Logical right shift one bit

Worked examples

Example 1 — a first encounter with Logical shift

Start with the simplest possible case. Write down what Logical shift claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Logical shift before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Logical shift ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Logical shift

In research
Logical shift appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Logical shift in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Logical shift is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary arithmetic, Operators (programming), so understanding it makes those chapters shorter.
In everyday life
Look for Logical shift outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Logical shift in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Logical shift means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Logical shift out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Logical shift in simple terms?

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.

Why does Logical shift matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Logical shift?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Logical shift.

Tags

  • Binary arithmetic
  • Operators (programming)

Keep exploring