How do I set a UInt32 to its maximum value
Asked Answered
I

6

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

Indurate answered 19/11, 2013 at 11:31 Comment(4)
Wouldn't that be 2^32 - 1?Forfeit
developer.apple.com/library/mac/documentation/Cocoa/Reference/…Station
Damn SO, you used to be so friendly. This is a valid question, why all the down votes? Haters got to hate, I wish they would do it somewhere else though.Indurate
(uint32_t)0-1 ;-)Sprain
C
84

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

Catalinacatalo answered 19/11, 2013 at 11:35 Comment(2)
You have my up vote already, but it may be helpful for future users if you list a few more of these macros like MAXFLOAT and similar.Station
@0x7fffffff You should take a look into <stdint.h>. There are virtually a few dozens of macros which define min, max for all signed and unsigned integer types and a few other macros. These are defined in the ISO/IEC 988:1999 specification.Catalinacatalo
G
19

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.

Gaynor answered 19/11, 2013 at 11:38 Comment(1)
Thanks for that, I was thinking maybe you could do some simple arithmetic on it (sizeof(UInt32) * 8) ^ 2. Be a bit of overkill, I was a bit sleepy when I asked.Indurate
R
4

The portable way:

std::numeric_limits<uint32_t>::max()
Ragucci answered 15/8, 2015 at 2:11 Comment(3)
why is this more portable?Indurate
More portable in comparison with some hardcoded values. Maybe uint32_t is not a great example. If you take std::numeric_limits<int32_t> for example, you do not have to make assumption on how signed numbers are coded on the machine. If you take std::numeric_limits<unsigned int>, you are not making any assumption about the size of an int which may not alway be 32 bit depending on the hardware that is targeted. It is also a bit more flexible than the macros. If you need to change the type, through a typedef for instance, you do not have to update all the UINT32_MAX like macros in your code.Ragucci
This answer is C++ code, but the question is about Objective C on iOS.Maudiemaudlin
H
4

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
}
Hendrick answered 10/9, 2016 at 14:46 Comment(0)
P
2

4_294_967_295 is the maximal value or in hexadecimal 0xFFFFFFFF.

Picul answered 19/11, 2013 at 11:36 Comment(0)
D
0

An alternative for any unsigned in C or C++ is:

anUnsigned = -1;

This is useful since it works for them all, so if you change from unsigned int to unsigned long you don't need to go through your code. You will also see this used in a lot of bit fiddling code:

anUnsigned |= -(aBoolOrConditionThatWhenTrueCausesAnUnsignedToBeSetToAll1s)
anUnsigned |= -(!aValueThatWhenZeroCausesAnUnsignedToBeSetToAll1s)
anUnsigned |= -(!!aValueThatWhenNonZeroCausesAnUnsignedToBeSetToAll1s)

The downside is that it looks odd, assigning a negative number to an unsigned!

Disposition answered 16/6, 2016 at 6:2 Comment(2)
sometimes doing it this way causes compiler warnings or static analysis warnings, pity because it works fine! :)Phosphorylase
To avoid a compiler warning and make it clear to other readers that you know what you're doing, cast the value to the correct type, e.g. anUnsigned = (uint32_t)-1; .Brazzaville

© 2022 - 2024 — McMap. All rights reserved.