0
0
Embedded Cprogramming~5 mins

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

Choose your learning style9 modes available
Recall & Review
beginner
What does the left shift operator (<<) do in C?
The left shift operator (<<) moves the bits of a number to the left by a specified number of positions, filling the right side with zeros. It effectively multiplies the number by 2 for each shift position.
Click to reveal answer
beginner
What happens to bits shifted out on the left side during a left shift?
Bits shifted out on the left side are discarded and lost. They do not wrap around or reappear on the right side.
Click to reveal answer
intermediate
How does the right shift operator (>>) behave for unsigned integers in C?
For unsigned integers, the right shift operator (>>) shifts bits to the right and fills the left side with zeros. This is called a logical right shift.
Click to reveal answer
intermediate
How does the right shift operator (>>) behave for signed integers in C?
For signed integers, the right shift operator (>>) usually performs an arithmetic right shift, which fills the left side with the sign bit (0 for positive, 1 for negative) to preserve the number's sign.
Click to reveal answer
advanced
What is a potential risk when shifting bits more than the width of the data type?
Shifting bits by a number equal to or greater than the width of the data type (e.g., 32 for a 32-bit int) leads to undefined behavior in C, which can cause unpredictable results.
Click to reveal answer
What does the expression (1 << 3) evaluate to in C?
A1
B3
C8
D0
In C, what happens when you right shift an unsigned int by 1?
ABits shift right, leftmost bit filled with 0
BBits shift right, leftmost bit filled with sign bit
CBits shift left, rightmost bit filled with 0
DUndefined behavior
What is the result of right shifting a negative signed integer in C?
AUndefined behavior
BLeft bits filled with 0
CBits rotate around
DLeft bits filled with 1 (sign bit), preserving sign
What happens if you shift an int by 32 bits on a 32-bit system?
AResult is always zero
BUndefined behavior
CBits rotate
DCompiler error
Which operator is used for left bit shifting in C?
A<<
B>>
C&
D|
Explain how left shift and right shift operators work in C for both signed and unsigned integers.
Think about how bits move and what fills the empty spaces.
You got /5 concepts.
    What are the risks of shifting bits incorrectly in embedded C programming?
    Consider what happens when shifting beyond data size or shifting signed values.
    You got /4 concepts.