Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a UInt32 to it's maximum value

  1. What is the maximum value for a UInt32?

  2. Is there a way I can use the sizeof operator to get the maximum value (as it is unsigned)? So I don't end up with #defines or magic numbers in my code.

like image 852
Aran Mulholland Avatar asked Nov 19 '13 11:11

Aran Mulholland


People also ask

What is max value of uint32?

Remarks. The value of this constant is 4,294,967,295; that is, hexadecimal 0xFFFFFFFF.

What is the max value of unsigned int?

The number 4,294,967,295, equivalent to the hexadecimal value FFFF,FFFF16, is the maximum value for a 32-bit unsigned integer in computing.

What is the max value of long?

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).


4 Answers

There's a macro UINT32_MAX defined in stdint.h which you can use

#include <stdint.h>

uint32_t max = UINT32_MAX;

More about the relevant header <stdint.h>:

http://pubs.opengroup.org/onlinepubs/009695299/basedefs/stdint.h.html

like image 130
CouchDeveloper Avatar answered Oct 17 '22 12:10

CouchDeveloper


The maximum value for UInt32 is 0xFFFFFFFF (or 4294967295 in decimal).

sizeof(UInt32) would not return the maximum value; it would return 4, the size in bytes of a 32 bit unsigned integer.

like image 20
liamnichols Avatar answered Oct 17 '22 11:10

liamnichols


Just set the max using standard hexadecimal notation and then check it against whatever you need. 32-bits is 8 hexadecimals bytes, so it'd be like this:

let myMax: UInt32 = 0xFFFFFFFF

if myOtherNumber > myMax {
    // resolve problem
}
like image 5
Ryan Dines Avatar answered Oct 17 '22 12:10

Ryan Dines


The portable way:

std::numeric_limits<uint32_t>::max()
like image 4
Renaud Avatar answered Oct 17 '22 11:10

Renaud