0
0
Embedded Cprogramming~20 mins

AND for masking bits in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bitwise AND Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this bit masking code?

Consider the following C code that uses the AND operator to mask bits. What will be printed?

Embedded C
unsigned char value = 0xAB;  // 10101011 in binary
unsigned char mask = 0x0F;   // 00001111 in binary
unsigned char result = value & mask;
printf("0x%X", result);
A0x0B
B0xAB
C0xAF
D0xA0
Attempts:
2 left
💡 Hint

AND operator keeps bits set only if both bits are 1.

Predict Output
intermediate
2:00remaining
What does this masking code output?

Given the code below, what is the printed output?

Embedded C
unsigned int flags = 0xF0F0;  // binary: 1111000011110000
unsigned int mask = 0x0FF0;  // binary: 0000111111110000
unsigned int masked = flags & mask;
printf("0x%X", masked);
A0x00F0F0
B0x00F0
C0F00x0
Dx00F0
Attempts:
2 left
💡 Hint

AND keeps bits where both are 1.

Predict Output
advanced
2:00remaining
What is the output of this masking with shifting?

What will this C code print?

Embedded C
unsigned char data = 0x5A;  // 01011010
unsigned char mask = 0x03;  // 00000011
unsigned char result = (data >> 2) & mask;
printf("%u", result);
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Shift right first, then mask with 0x03.

Predict Output
advanced
2:00remaining
What is the output of this combined mask?

What does this code print?

Embedded C
unsigned int x = 0x12345678;
unsigned int mask = 0x00FF00FF;
unsigned int result = x & mask;
printf("0x%X", result);
A0x00340078
B0x12005678
C0x12340000
D0x00345600
Attempts:
2 left
💡 Hint

AND keeps bits where mask bits are 1.

Predict Output
expert
2:00remaining
What is the output of this bit masking with signed integer?

Consider this code snippet. What will it print?

Embedded C
int x = -42;  // Two's complement representation
unsigned int mask = 0xFF;
unsigned int result = x & mask;
printf("0x%X", result);
A0xFFFFFF2A
B0x2A
C0xFFFFFFD6
D0xD6
Attempts:
2 left
💡 Hint

Masking with 0xFF keeps the lowest 8 bits.