Bird
Raised Fist0
Embedded Cprogramming~10 mins

AND for masking bits in Embedded C - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mask the lower 4 bits of the variable value.

Embedded C
unsigned char masked = value [1] 0x0F;
Drag options to blanks, or click blank then click option'
A|
B^
C~
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR (|) instead of AND (&) will not mask bits correctly.
Using XOR (^) changes bits instead of masking.
Using NOT (~) inverts bits, not mask.
2fill in blank
medium

Complete the code to mask bits 4 to 7 (upper nibble) of value.

Embedded C
unsigned char masked = value [1] 0xF0;
Drag options to blanks, or click blank then click option'
A&
B<<
C^
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR (|) will set bits instead of masking.
Using shift operators (<<) alone does not mask bits.
Using XOR (^) flips bits, not mask.
3fill in blank
hard

Fix the error in the code to mask bits 2 and 3 of value.

Embedded C
unsigned char masked = value [1] 0x0C;
Drag options to blanks, or click blank then click option'
A|
B&
C<<
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR (|) sets bits instead of masking.
Using shift operators (<<, >>) do not mask bits directly.
4fill in blank
hard

Fill both blanks to create a mask that keeps bits 1, 3, and 5 of value.

Embedded C
unsigned char mask = [1];
unsigned char masked = value [2] mask;
Drag options to blanks, or click blank then click option'
A0x2A
B&
C|
D0x15
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR (|) instead of AND (&) to apply the mask.
Using wrong mask value that does not match bits 1, 3, and 5.
5fill in blank
hard

Fill all three blanks to mask bits 0, 2, and 4 of value and store the result in masked.

Embedded C
unsigned char mask = [1];
unsigned char masked = value [2] mask [3] 0;
Drag options to blanks, or click blank then click option'
A0x15
B&
C+
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR (|) instead of AND (&) to mask bits.
Using wrong mask value.
Adding a non-zero value changes the result.

Practice

(1/5)
1. What does the bitwise AND operation with a mask do in embedded C?
easy
A. It adds the mask value to the original number.
B. It keeps only the bits where the mask has 1s and turns off others.
C. It flips all bits in the original number.
D. It shifts the bits to the left by the mask value.

Solution

  1. Step 1: Understand bitwise AND with mask

    The AND operation compares each bit of two numbers and returns 1 only if both bits are 1.
  2. Step 2: Apply mask logic

    Using a mask with 1s keeps those bits from the original number; 0s turn off bits.
  3. Final Answer:

    It keeps only the bits where the mask has 1s and turns off others. -> Option B
  4. Quick Check:

    AND with mask = keep bits [OK]
Hint: Mask with 1 keeps bits; 0 clears bits [OK]
Common Mistakes:
  • Thinking AND flips bits
  • Confusing AND with addition
  • Assuming AND shifts bits
2. Which of the following is the correct syntax to mask the lower 4 bits of a variable val in embedded C?
easy
A. val & 0xF
B. val | 0xF
C. val << 4
D. val ^ 0xF

Solution

  1. Step 1: Identify mask for lower 4 bits

    Lower 4 bits mask is 0xF (binary 00001111).
  2. Step 2: Apply bitwise AND syntax

    Use val & 0xF to keep only lower 4 bits.
  3. Final Answer:

    val & 0xF -> Option A
  4. Quick Check:

    AND with 0xF masks lower 4 bits [OK]
Hint: Use & with 0xF to keep lower 4 bits [OK]
Common Mistakes:
  • Using OR instead of AND
  • Using XOR which toggles bits
  • Using shift instead of mask
3. What is the output of the following code?
unsigned char val = 0xAB;  // 10101011 in binary
unsigned char mask = 0x0F; // 00001111 in binary
unsigned char result = val & mask;
printf("%02X", result);
medium
A. 0B
B. AF
C. 0A
D. AB

Solution

  1. Step 1: Convert values to binary

    val = 10101011, mask = 00001111.
  2. Step 2: Perform bitwise AND

    10101011 & 00001111 = 00001011 (binary) = 0x0B (hex).
  3. Final Answer:

    0B -> Option A
  4. Quick Check:

    AND keeps lower 4 bits = 0B [OK]
Hint: AND keeps bits where mask is 1 [OK]
Common Mistakes:
  • Reading hex incorrectly
  • Confusing mask bits
  • Assuming output is original val
4. The following code is intended to mask the upper 4 bits of val, but it does not work as expected. What is the error?
unsigned char val = 0xAB;  // 10101011
unsigned char mask = 0xF0; // 11110000
unsigned char result = val | mask;
printf("%X", result);
medium
A. Mask value is incorrect
B. Missing parentheses around expression
C. Variable types are wrong
D. Using OR (|) instead of AND (&) for masking

Solution

  1. Step 1: Understand masking operation

    Masking to keep bits requires AND (&), not OR (|).
  2. Step 2: Identify incorrect operator

    The code uses OR which sets bits instead of masking.
  3. Final Answer:

    Using OR (|) instead of AND (&) for masking -> Option D
  4. Quick Check:

    Masking needs AND, not OR [OK]
Hint: Mask bits with & not | [OK]
Common Mistakes:
  • Using OR instead of AND
  • Changing mask value unnecessarily
  • Assuming parentheses fix bitwise errors
5. You have a 16-bit sensor data stored in uint16_t data. The upper 8 bits represent temperature, and the lower 8 bits represent humidity. How do you extract only the temperature value using AND masking?
hard
A. temperature = (data | 0xFF00) >> 8;
B. temperature = data & 0x00FF;
C. temperature = (data & 0xFF00) >> 8;
D. temperature = data >> 8;

Solution

  1. Step 1: Identify mask for upper 8 bits

    Upper 8 bits mask is 0xFF00 (binary 11111111 00000000).
  2. Step 2: Apply AND and shift right

    Use (data & 0xFF00) to keep upper bits, then shift right 8 to get value.
  3. Final Answer:

    temperature = (data & 0xFF00) >> 8; -> Option C
  4. Quick Check:

    Mask upper 8 bits and shift right [OK]
Hint: Mask upper bits with 0xFF00 then shift right 8 [OK]
Common Mistakes:
  • Masking lower bits instead
  • Using OR instead of AND
  • Forgetting to shift after masking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard