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:
What is the use of/benefit in using a uint16 where an uint32 will also suffice(if, there is any)?
Will there be any savings in memory usage in using shorter data types (considering data alignment)?
If it is to save a few bytes of memory, is it something sensible to do in modern hardware?
UInt8 == 8 bit == 1 byte, UInt16 == 16 bit == 2 byte, UInt32 == 32 bit == 4 byte.
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.
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.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With