Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between uint8_t, uint_fast8_t and uint_least8_t

Tags:

c++

c

integer

c99

avr

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?

like image 569
mic Avatar asked Jan 28 '16 07:01

mic


People also ask

What is the difference between UInt8 and uint8_t?

The difference between Uint8 and uint8_t will depend on implementation, but usually they will both be 8 bit unsigned integers. Also uint8_t and uint16_t are defined by C (and maybe C++) standard in stdint. h header, Uint8 and Uint16 are non-standard as far as I know.

What is the difference between uint8_t and char?

If the intended use of the variable is to hold an unsigned numerical value, use uint8_t; If the intended use of the variable is to hold a signed numerical value, use int8_t; If the intended use of the variable is to hold a printable character, use char.

What is type uint8_t?

Unsigned Integers of 8 bits. A uint8 data type contains all whole numbers from 0 to 255. As with all unsigned numbers, the values must be non-negative. Uint8's are mostly used in graphics (colors are always non-negative).

What is int8_t and uint8_t?

uint8_t and int8_t are optional integer types of exactly 8 bits. Both types have no padding, and int8_t uses 2's complement. uint8_t is unsigned, and has the range zero to UINT8_MAX , which is [0, +255]. int8_t is signed, and has the range INT8_MIN to INT8_MAX , which is [−128, +127].


1 Answers

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: is unsigned char, the smallest type that is at least 8 bits.
  • uint_fast8_t: is unsigned int, because in my imaginary architecture, a half-register variable is slower than a full-register one.
like image 64
rodrigo Avatar answered Oct 19 '22 04:10

rodrigo