0
0
Embedded Cprogramming~5 mins

AND for masking bits in Embedded C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the AND operator (&) do when used for masking bits in embedded C?
The AND operator (&) keeps bits set to 1 only if both bits in the operands are 1. It is used to isolate specific bits by masking others to 0.
Click to reveal answer
beginner
How do you use AND to check if a specific bit is set in a byte?
Use AND with a mask that has 1 only at the bit position you want to check. If the result is not zero, the bit is set.
Click to reveal answer
beginner
Example: What is the result of 0b1101 & 0b0100 in binary?
0b0100 (which is 4 in decimal). Only the third bit is kept because the mask has 1 there.
Click to reveal answer
intermediate
Why is AND masking useful in embedded programming?
It allows you to read or clear specific bits in hardware registers without changing other bits.
Click to reveal answer
beginner
What happens if you AND a number with 0xFF?
The number stays the same because 0xFF has all bits set to 1 in the lowest 8 bits, so no bits are masked out.
Click to reveal answer
What does the expression (value & 0x01) check in embedded C?
AIf value is zero
BIf the most significant bit of value is set
CIf all bits of value are set
DIf the least significant bit of value is set
What is the result of 0b1010 & 0b1100?
A0b1110
B0b0110
C0b1000
D0b0010
Which mask would you use to isolate the 4th bit (bit 3) of a byte?
A0x08
B0x04
C0x10
D0x01
If you want to clear all bits except bit 2, which operation would you use?
Avalue ^ 0x04
Bvalue & 0x04
Cvalue | 0x04
Dvalue & 0xFB
What does ANDing a number with zero do?
AAlways results in zero
BKeeps the number unchanged
CInverts the bits
DSets all bits to 1
Explain how the AND operator is used to mask bits in embedded C.
Think about how you keep some bits and clear others.
You got /5 concepts.
    Describe a real-life example where masking bits with AND is useful in embedded programming.
    Imagine reading a button press or sensor status.
    You got /4 concepts.