0
0
Cprogramming~5 mins

Left shift and right shift in C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A12
B6
C1
D8
What does the right shift operator (>>) do to the bits of an unsigned integer?
AFills left bits with zeros
BFills left bits with ones
CReverses the bits
DSwaps the bits
Which operator would you use to multiply an integer by 8 using bit shifts?
A<< 2
B>> 3
C<< 3
D>> 2
What happens if you left shift a signed integer into its sign bit?
ANo change
BAlways zero
CAlways positive
DUndefined behavior or negative value
If int x = -16; what might x >> 2 do?
ADivide by 4 and fill with zeros
BDivide by 4 and keep the sign
CMultiply by 4
DCause a compile error
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.