Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of uint8, uint16 etc

Tags:

Currently I am working with a code base (C, C++ mixed) targeted for a 32 bit MIPS platform. The processor is a fairly modern one [just to mention that we have a good amount of processing power and memory].

The code base uses data types like uint8[1 byte wide unsigned integer], uint16[2 byte wide unsigned integer], uint32[4 byte wide unsigned integer] etc.

I know how the usage of these constructs are helpful while porting the code to different platforms.

My questions are:

  1. What is the use of/benefit in using a uint16 where an uint32 will also suffice(if, there is any)?

  2. Will there be any savings in memory usage in using shorter data types (considering data alignment)?

  3. If it is to save a few bytes of memory, is it something sensible to do in modern hardware?

like image 586
NeonGlow Avatar asked Feb 25 '13 07:02

NeonGlow


People also ask

What is the difference between UINT8 and UInt16?

UInt8 == 8 bit == 1 byte, UInt16 == 16 bit == 2 byte, UInt32 == 32 bit == 4 byte.

What is UInt16 in C?

The UInt16 value type represents unsigned integers with values ranging from 0 to 65535. Important. The UInt16 type is not CLS-compliant. The CLS-compliant alternative type is Int32. Int16 can be used instead to replace a UInt16 value that ranges from zero to Int16.

What is UINT8 in C?

A UINT8 is an 8-bit unsigned integer (range: 0 through 255 decimal). Because a UINT8 is unsigned, its first bit (Most Significant Bit (MSB)) is not reserved for signing.

Is UINT8 a data type?

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).


1 Answers

What is the use of/benefit in using a uint16 where an uint32 will also suffice(if, there is any)?

If those uint16s are parts of arrays or structures, you can save memory and perhaps be able to handle larger data sets than with uint32s in those same arrays or structures. It really depends on your code.

Data protocols and file formats may use uint16s and it may not be correct to use uint32s instead. This depends on the format and semantics (e.g. if you need values to wrap around from 65535 to 0, uint16 will do that automatically while uint32 won't).

OTOH, if those uint16s are just single local or global variables, replacing them with 32-bit ones might make no significant difference because they are likely to occupy the same space due to alignment and they are passed as 32-bit parameters (on the stack or in registers) on MIPS anyway.

Will there be any savings in memory usage in using shorter data types (considering data alignment)?

There may be savings, especially when uint16s are parts of many structures or elements of big arrays.

If it is to save a few bytes of memory, is it something sensible to do in modern hardware?

Yes, you lower the memory bandwidth (which is always a good thing) and you often lower various cache misses (data caches and TLB) when you operate on less data.

like image 164
Alexey Frunze Avatar answered Oct 05 '22 08:10

Alexey Frunze