Loading...
A shift changes a fixed-width pattern; its numerical meaning depends on signedness and which bits are preserved. Compare the exact mathematical result with the permitted range.
What you'll be able to do
A logical right shift fills new high positions with zeroes. An arithmetic right shift of a two’s-complement value repeats its sign bit. For eight-bit −6, pattern 11111010, shifting two positions logically gives 00111110=62, while arithmetic shifting gives 11111110=−2. These are different results from the same source bits.
Under this two’s-complement arithmetic-right-shift model, the signed result follows division by 2² with rounding down: −6/4=−1.5 becomes −2. It is not truncation towards zero. Read the question’s convention rather than importing an unrelated language’s shift rule. A left shift inserts low zeroes in both the usual logical and arithmetic pattern operation, but can lose high bits.
For eight-bit unsigned storage the range is 0 through 255. The true sum 255+1=256 exceeds the maximum; retaining only eight bits gives zero but loses the exact result. A result below the permitted lower bound is also a range failure: 0−1 cannot be represented as a non-negative unsigned value, even though a wrapped machine pattern may be 11111111.
Classroom discussions may call falling below a lower bound underflow. In machine arithmetic, signed range failures can also be described as overflow conditions. In the later IEEE floating-point topic, underflow has a different emphasis: tiny nonzero magnitudes approach the format’s subnormal/zero limit. State the representation and intended meaning instead of treating every negative value or every discarded fractional bit as underflow.
Eight-bit signed +64 is 01000000. A one-place left shift retains 10000000, interpreted as −128. The mathematical doubled result +128 lies above the signed maximum +127, so the pattern is not an exact signed multiplication result. The same retained pattern is unsigned 128 if the task uses that different interpretation.
A safe shift argument names the original value, width, signedness, fill rule, discarded bits and decoded result. Sign extension while widening a negative value is different from shifting it within the same width. Use examples at zero, the signed endpoints and just beyond each range to check whether a claimed conversion or arithmetic operation meets its contract.
| Source: eight-bit −6 | Result bits after right shift by 2 | Interpretation |
|---|---|---|
| Logical zero fill | 00111110 | Unsigned/signed positive 62 |
| Arithmetic sign fill | 11111110 | Signed −2 |
Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.