0
0
Embedded Cprogramming~5 mins

Checking if a bit is set in Embedded C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to check if a bit is set in a number?
It means to find out if a specific bit position in a number is 1 (on) or 0 (off).
Click to reveal answer
beginner
Which operator is commonly used in C to check if a bit is set?
The bitwise AND operator (&) is used to check if a bit is set.
Click to reveal answer
intermediate
How do you check if the 3rd bit (counting from 0) is set in a variable 'x'?
Use the expression: (x & (1 << 3)) != 0. This shifts 1 to the 3rd bit and ANDs with x.
Click to reveal answer
beginner
Why do we use (1 << n) when checking bits?
Because (1 << n) creates a number with only the nth bit set to 1, which helps isolate that bit.
Click to reveal answer
beginner
What will the expression (x & (1 << n)) return if the nth bit is not set?
It will return 0, meaning the bit is off.
Click to reveal answer
What does the expression (x & (1 << 2)) check?
AIf the 2nd bit of x is set
BIf the 1st bit of x is set
CIf the 3rd bit of x is set
DIf x is equal to 2
What operator is used to isolate a bit in embedded C?
A+
B|
C&
D^
If (x & (1 << n)) equals zero, what does it mean?
AThe nth bit is set
BThe nth bit is not set
Cx is zero
Dn is zero
What does (1 << 5) do in C?
AClears bit 5
BSets the 5th bit to 1
CChecks if bit 5 is set
DShifts 1 left by 5 bits
Which of these is a correct way to check if bit 0 is set in variable x?
Ax & 1
Bx | 1
Cx ^ 1
Dx << 1
Explain how to check if a specific bit is set in a number using embedded C.
Think about isolating one bit using a mask.
You got /4 concepts.
    Describe why shifting 1 by n positions helps in checking bits.
    Imagine turning on only one light in a row of lights.
    You got /3 concepts.