Bird
Raised Fist0
Embedded Cprogramming~20 mins

I2C addressing (7-bit and 10-bit) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
I2C Addressing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of 7-bit I2C address shifting
What is the output of this code snippet that shifts a 7-bit I2C address to form the 8-bit address for write operation?
Embedded C
uint8_t address_7bit = 0x3A; // 7-bit address
uint8_t address_8bit = address_7bit << 1; // Shift left by 1 for write
printf("0x%02X", address_8bit);
A0x75
B0x3A
C0x74
D0x1D
Attempts:
2 left
💡 Hint
Remember that the 7-bit address is shifted left by 1 bit to make room for the read/write bit.
❓ Predict Output
intermediate
2:00remaining
10-bit I2C address first byte calculation
Given a 10-bit I2C address 0x345, what is the value of the first byte sent on the bus according to the I2C 10-bit addressing protocol?
Embedded C
uint16_t address_10bit = 0x345;
uint8_t first_byte = 0xF0 | ((address_10bit >> 7) & 0x06); // bits 9 and 8 shifted
printf("0x%02X", first_byte);
A0xF6
B0xF4
C0xF2
D0xF0
Attempts:
2 left
💡 Hint
The first byte starts with 11110 and includes bits 9 and 8 of the address in bits 2 and 1.
🔧 Debug
advanced
2:00remaining
Identify the error in 7-bit I2C address usage
What error will this code cause when used to send a 7-bit I2C address on the bus?
Embedded C
uint8_t address_7bit = 0x50;
uint8_t address_8bit = address_7bit | 0x01; // Attempt to set read bit
// Send address_8bit on bus
AThe code will cause a compile-time error due to invalid bitwise operation
BThe address is incorrect because it does not shift the 7-bit address left before adding the read bit
CThe code correctly forms the 8-bit address for read operation
DThe code will cause a runtime error due to overflow
Attempts:
2 left
💡 Hint
Think about how the 7-bit address must be shifted before adding the read/write bit.
📝 Syntax
advanced
2:00remaining
Syntax error in 10-bit I2C address byte formation
Which option contains a syntax error when trying to form the second byte of a 10-bit I2C address?
Embedded C
uint16_t addr = 0x2AA;
uint8_t second_byte = addr & 0xFF;
Auint8_t second_byte = addr & 0xFF
Buint8_t second_byte = (uint8_t)(addr & 0xFF);
Cuint8_t second_byte = addr & 0xFF;
Duint8_t second_byte = (addr & 0xFF);
Attempts:
2 left
💡 Hint
Check for missing semicolons or incorrect casting syntax.
🚀 Application
expert
2:00remaining
Calculate total bytes sent for 10-bit I2C address write
How many bytes are sent on the I2C bus when writing to a device with a 10-bit address, including address and data bytes, if the data payload is 3 bytes?
A3 bytes
B4 bytes
C6 bytes
D5 bytes
Attempts:
2 left
💡 Hint
Remember that 10-bit addressing requires two address bytes plus the data bytes.

Practice

(1/5)
1.

What is the size of the standard I2C address used to identify devices?

easy
A. 7 bits
B. 8 bits
C. 10 bits
D. 16 bits

Solution

  1. Step 1: Understand I2C addressing basics

    I2C commonly uses 7-bit addresses to identify devices on the bus.
  2. Step 2: Recall standard address size

    The standard I2C address size is 7 bits, while 10-bit is an extended mode.
  3. Final Answer:

    7 bits -> Option A
  4. Quick Check:

    Standard I2C address = 7 bits [OK]
Hint: Remember standard I2C uses 7-bit addresses [OK]
Common Mistakes:
  • Confusing 7-bit with 8-bit addresses
  • Thinking 10-bit is the default
  • Assuming 16-bit addresses exist in I2C
2.

Which of the following is the correct way to prepare a 7-bit I2C address for a write operation in embedded C?

uint8_t address = 0x3C; // 7-bit address
uint8_t write_address = ?;
easy
A. write_address = address << 2;
B. write_address = (address >> 1) | 1;
C. write_address = address | 0x80;
D. write_address = (address << 1) | 0;

Solution

  1. Step 1: Shift 7-bit address left by 1

    The 7-bit address must be shifted left by 1 to make room for the read/write bit.
  2. Step 2: Set read/write bit for write operation

    For write, the least significant bit is 0, so OR with 0.
  3. Final Answer:

    write_address = (address << 1) | 0; -> Option D
  4. Quick Check:

    Shift left 1 + write bit 0 = correct address [OK]
Hint: Shift left by 1, OR with 0 for write [OK]
Common Mistakes:
  • Shifting right instead of left
  • Setting read/write bit incorrectly
  • Using wrong bitwise OR values
3.

Given the following code snippet, what is the value of header for a 10-bit I2C address 0x305?

uint16_t addr = 0x305;
uint8_t header = 0xF0 | ((addr >> 7) & 0x06) | 0x00; // write operation
medium
A. 0xF6
B. 0xF0
C. 0xF2
D. 0xF4

Solution

  1. Step 1: Analyze addr = 0x305

    Binary 0b1100000101 (bits 9-0: bit9=1, bit8=1, bit7=0). Shift right 7: 0b110 (6).
  2. Step 2: Mask and compute header

    (6 & 0x06) = 0x06. Then 0xF0 | 0x06 | 0x00 = 0xF6.
  3. Final Answer:

    0xF6 -> Option A
  4. Quick Check:

    0xF0 | ((0x305 >> 7) & 0x06) = 0xF6 [OK]
Hint: Shift address right 7, mask with 0x06, OR with 0xF0 [OK]
Common Mistakes:
  • Not shifting address before masking
  • Incorrect mask value
  • Confusing read/write bit in header
4.

Identify the error in this code snippet for sending a 10-bit I2C address:

uint16_t addr = 0x3FF;
uint8_t header = 0xF0 | ((addr >> 7) & 0x06) | 0x01; // read operation
uint8_t low_addr = addr & 0xFF;
// send header and low_addr over I2C
medium
A. The read/write bit in header should be 0 for read
B. The low_addr should be shifted left by 1 before sending
C. The header calculation is correct; no error
D. The mask 0x06 should be 0x07 to include all bits

Solution

  1. Step 1: Check read/write bit in header

    For read operation, the last bit is set to 1, which matches the code.
  2. Step 2: Verify mask and low address

    Mask 0x06 correctly extracts bits 7 and 8; low_addr is the lower 8 bits, no shift needed.
  3. Final Answer:

    The header calculation is correct; no error -> Option C
  4. Quick Check:

    Header and low_addr correct for 10-bit read [OK]
Hint: Check read/write bit and mask carefully [OK]
Common Mistakes:
  • Confusing read/write bit value
  • Incorrect mask for bits 7 and 8
  • Shifting low address unnecessarily
5.

You need to write a function in embedded C that takes a 10-bit I2C address and returns the two bytes to send: the header byte and the low address byte for a write operation. Which of the following implementations is correct?

void get_i2c_10bit_bytes(uint16_t addr, uint8_t *header, uint8_t *low_addr) {
    // ?
}
hard
A. header = 0xF0 | ((addr >> 7) & 0x06) | 0x00; low_addr = addr & 0xFF;
B. *header = 0xF0 | ((addr >> 7) & 0x06) | 0x00; *low_addr = addr & 0xFF;
C. *header = 0xF0 | ((addr << 7) & 0x06) | 0x00; *low_addr = addr >> 8;
D. header = 0xF0 | ((addr >> 7) & 0x07) | 0x01; low_addr = addr & 0xFF;

Solution

  1. Step 1: Use pointers to assign output values

    The function parameters header and low_addr are pointers, so use * to assign values.
  2. Step 2: Calculate header and low_addr correctly

    Header is 0xF0 ORed with bits 7 and 8 of addr shifted right 7, and OR 0 for write; low_addr is lower 8 bits.
  3. Final Answer:

    *header = 0xF0 | ((addr >> 7) & 0x06) | 0x00; *low_addr = addr & 0xFF; -> Option B
  4. Quick Check:

    Pointer dereference + correct bit ops = correct function [OK]
Hint: Use * to assign pointer outputs, shift right 7 bits [OK]
Common Mistakes:
  • Not using * to assign pointer values
  • Shifting bits incorrectly
  • Setting read/write bit wrong
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