Fixed-width integers (uint8_t, uint16_t, uint32_t) in Embedded C - Time & Space Complexity
When working with fixed-width integers like uint8_t, uint16_t, and uint32_t, it's important to understand how operations on these types affect program speed.
We want to see how the time to run code changes as the input size or number of operations grows.
Analyze the time complexity of the following code snippet.
#include <stdint.h>
void increment_array(uint8_t *arr, uint16_t size) {
for (uint16_t i = 0; i < size; i++) {
arr[i]++;
}
}
This code increases each element in an array of 8-bit unsigned integers by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Incrementing each element in the array.
- How many times: Exactly once for each element, controlled by the size variable.
As the array size grows, the number of increments grows at the same pace.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 increments |
| 100 | 100 increments |
| 1000 | 1000 increments |
Pattern observation: The work grows directly with the number of elements; doubling the size doubles the work.
Time Complexity: O(n)
This means the time to complete the task grows in a straight line with the number of elements.
[X] Wrong: "Using smaller fixed-width integers like uint8_t makes the loop run faster because each element is smaller."
[OK] Correct: The loop runs once per element regardless of integer size; the size affects memory but not how many times the loop runs.
Understanding how loops over fixed-width integers scale helps you explain performance clearly and shows you know how data size relates to processing time.
"What if we changed the array type from uint8_t to uint32_t but kept the same size? How would the time complexity change?"
Practice
uint16_t type in embedded C?Solution
Step 1: Understand the meaning of
The suffixuint16_t16_tmeans it uses 16 bits to store the number.Step 2: Confirm the size
By definition,uint16_tis an unsigned integer type that uses exactly 16 bits.Final Answer:
16 bits -> Option DQuick Check:
uint16_t= 16 bits [OK]
- Confusing
uint16_twithuint8_t - Thinking it stores 16 bytes instead of bits
- Mixing signed and unsigned types
count?Solution
Step 1: Identify the correct type for 32-bit unsigned integer
uint32_tis the standard fixed-width unsigned 32-bit integer type.Step 2: Check the variable declaration syntax
The syntaxuint32_t count;correctly declares the variablecountwith that type.Final Answer:
uint32_t count; -> Option BQuick Check:
32-bit unsigned integer declaration =uint32_t count;[OK]
uint32_t for 32-bit unsigned integers [OK]- Using signed type
int32_tinstead of unsigned - Using wrong bit size like
uint16_t - Incorrect variable naming or syntax
#include
#include
int main() {
uint8_t a = 250;
uint8_t b = 10;
uint8_t c = a + b;
printf("%u", c);
return 0;
}Solution
Step 1: Understand the variable types and values
aandbareuint8_t, which can hold values 0-255.a=250,b=10.Step 2: Calculate the sum and consider overflow
Sum is 250 + 10 = 260. Sinceuint8_tmax is 255, it wraps around modulo 256: 260 - 256 = 4.Final Answer:
4 -> Option CQuick Check:
260 mod 256 = 4 [OK]
- Expecting 260 as output ignoring overflow
- Thinking it causes runtime error
- Confusing signed and unsigned overflow behavior
#include uint8_t x = 300;
Solution
Step 1: Check the value assigned to
uint8_tuint8_tcan only hold values 0 to 255. 300 is outside this range.Step 2: Understand the effect of assigning 300
Assigning 300 causes overflow and the stored value wraps modulo 256, resulting in 44 (300 - 256).Final Answer:
300 is too large for uint8_t and causes overflow -> Option AQuick Check:
Value 300 > max 255 foruint8_t[OK]
- Assuming compiler error instead of overflow
- Ignoring the max value limit of uint8_t
- Thinking variable name or syntax is wrong
Solution
Step 1: Check the value range of each type
uint8_tmax is 255,uint16_tmax is 65535,uint32_tmax is 4294967295,int16_tmax is 32767.Step 2: Find the type that can hold 70000
70000 is greater than 65535 and 32767, so onlyuint32_tcan hold it without overflow.Final Answer:
uint32_t -> Option AQuick Check:
70000 <= 4294967295 ofuint32_t[OK]
- Choosing
uint16_twhich is too small - Using signed types that have smaller max positive values
- Ignoring overflow limits
Ready to prove it?
Pass the test and this pattern moves unknown
