Recall & Review
beginner
What does the left shift operator (<<) do in C?
It moves the bits of a number to the left by a specified number of positions, effectively multiplying the number by 2 for each shift.
Click to reveal answer
beginner
What is the effect of the right shift operator (>>) in C?
It moves the bits of a number to the right by a specified number of positions, effectively dividing the number by 2 for each shift (for unsigned numbers).
Click to reveal answer
beginner
Explain what happens when you do: int x = 4; int y = x << 1;
The value of x (4) is shifted left by 1 bit, which multiplies it by 2. So y becomes 8.
Click to reveal answer
intermediate
What is a potential risk when using left shift on signed integers?
Shifting bits into the sign bit can cause undefined behavior or unexpected negative values.
Click to reveal answer
intermediate
How does right shift behave differently for signed and unsigned integers in C?
For unsigned integers, right shift fills with zeros (logical shift). For signed integers, it may fill with the sign bit (arithmetic shift), preserving the sign.
Click to reveal answer
What is the result of 3 << 2 in C?
✗ Incorrect
3 in binary is 00000011. Shifting left by 2 bits gives 00001100, which is 12.
What does the right shift operator (>>) do to the bits of an unsigned integer?
✗ Incorrect
Right shift on unsigned integers fills the left bits with zeros (logical shift).
Which operator would you use to multiply an integer by 8 using bit shifts?
✗ Incorrect
Shifting left by 3 bits multiplies the number by 2^3 = 8.
What happens if you left shift a signed integer into its sign bit?
✗ Incorrect
Shifting into the sign bit can cause undefined behavior or change the sign unexpectedly.
If int x = -16; what might x >> 2 do?
✗ Incorrect
Right shift on signed integers usually performs arithmetic shift, preserving the sign.
Explain how left shift and right shift operators work in C and give a simple example for each.
Think about how shifting bits changes the number's value.
You got /4 concepts.
What are the differences in behavior of right shift on signed versus unsigned integers in C?
Consider how negative numbers are handled.
You got /4 concepts.