Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an Uint32 and an unsigned int in C++?

Tags:

c++

types

Is it the same? If no: what is the difference? If yes then why do you need this type?

like image 305
user1511417 Avatar asked Feb 16 '13 15:02

user1511417


People also ask

Is unsigned int same as UInt32?

unsigned int is the unsigned integer type, and will match in size with int . That means that it's at least 16 bits in size, although on most modern+popular platforms, it'll be 32 bits. Uint32 (or uint32_t ) is a 32-bit unsigned integer.

What is UInt32 in C?

uint32_t is a numeric type that guarantees 32 bits. The value is unsigned, meaning that the range of values goes from 0 to 232 - 1.

Is unsigned long same as uint32_t?

Type of unsigned long is different from uint32_t and uint64_t on Windows (VS2010) Bookmark this question. Show activity on this post. assuming of course that long is not longer than 64bits.

What is the difference between unsigned and unsigned int?

There is no difference. unsigned and unsigned int are both synonyms for the same type (the unsigned version of the int type).


1 Answers

uint32_t (or however pre-C++11 compilers call it) is guaranteed to be a 32-bit unsigned integer; unsigned int is whatever unsigned integer the compiler likes best to call unsigned int, as far as it meets the requirements of the standard (which demands for it a 0-65535 minimum range).

Like int, unsigned int typically is an integer that is fast to manipulate for the current architecture (normally it fits into a register), so it's to be used when a "normal", fast integer is required.

uint32_t, instead, is used when you need an exact-width integer, e.g. to serialize to file, or when you require that exact range or you rely on unsigned overflow to happen exactly at 2^32-1.

For example, on a 16 bit-processor unsigned int will typically be 16 bits wide, while uint32_t will have to be 32 bits wide.


Incidentally, as pointed out by @Marc Glisse, while unsigned int is always present, uint32_t is not mandatory - a particular compiler implementation may not provide it. This is mostly because not all platforms can easily provide such a type (typically DSPs with weird word sizes).

like image 168
Matteo Italia Avatar answered Sep 18 '22 12:09

Matteo Italia