Difference between uint8_t, uint_fast8_t and uint_least8_t
The C99 standard introduces the following datatypes. The documentation can be found here for the AVR stdint library.
-
uint8_t
means it's an 8-bit unsigned type. -
uint_fast8_t
means it's the fastest unsigned int with at least 8 bits. -
uint_least8_t
means it's an unsigned int with at least 8 bits.
I understand uint8_t
and what is uint_fast8_t
( I don't know how it's implemented in register level).
1.Can you explain what is the meaning of "it's an unsigned int
with at least 8 bits"?
2.How uint_fast8_t
and uint_least8_t
help increase efficiency/code space compared to the uint8_t
?
uint_least8_t
is the smallest type that has at least 8 bits.
uint_fast8_t
is the fastest type that has at least 8 bits.
You can see the differences by imagining exotic architectures. Imagine a 20-bit architecture. Its unsigned int
has 20 bits (one register), and its unsigned char
has 10 bits. So sizeof(int) == 2
, but using char
types requires extra instructions to cut the registers in half. Then:
-
uint8_t
: is undefined (no 8 bit type). -
uint_least8_t
: isunsigned char
, the smallest type that is at least 8 bits. -
uint_fast8_t
: isunsigned int
, because in my imaginary architecture, a half-register variable is slower than a full-register one.
uint8_t
means: give me an unsigned int of exactly 8 bits.
uint_least8_t
means: give me the smallest type of unsigned int which has at least 8 bits. Optimize for memory consumption.
uint_fast8_t
means: give me an unsigned int of at least 8 bits. Pick a larger type if it will make my program faster, because of alignment considerations. Optimize for speed.
Also, unlike the plain int
types, the signed version of the above stdint.h types are guaranteed to be 2's complement format.