如何将UInt32设置为最大值?

时间:2021-07-02 15:49:13
  1. What is the maximum value for a UInt32?

    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.

    是否有一种方法可以让我使用sizeof运算符获取最大值(因为它是无符号的)?因此,我不会在代码中使用#define或magic numbers。

6 个解决方案

#1


30  

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

在stdint中定义了一个宏UINT32_MAX。你可以用它。

#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

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

#2


8  

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

UInt32的最大值是0xFFFFFFFF(或4294967295)。

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

sizeof(UInt32)不会返回最大值;它将返回4,大小为一个32位无符号整数的字节。

#3


2  

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:

只需用标准十六进制表示法设置最大值,然后根据需要检查它。32位是8个十六进制字节,所以它是这样的:

let myMax: UInt32 = 0xFFFFFFFF

if myOtherNumber > myMax {
    // resolve problem
}

#4


0  

4.294.967.295 is the maximal value or in hexadecimal 0xFFFFFFFF

4.294.967.295是最大的值,或者是十六进制的0xFFFFFFFF。

#5


0  

The portable way:

便携式道:

std::numeric_limits<uint32_t>::max()

#6


0  

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

在C或c++中任何未签名的选项都是:

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:

这是有用的,因为它对所有人都有效,所以如果您从unsigned int更改为unsigned long,您不需要检查代码。你也会看到这个应用在很多琐碎的代码中:

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

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

缺点是它看起来很奇怪,给未签名的人分配一个负数!

#1


30  

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

在stdint中定义了一个宏UINT32_MAX。你可以用它。

#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

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

#2


8  

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

UInt32的最大值是0xFFFFFFFF(或4294967295)。

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

sizeof(UInt32)不会返回最大值;它将返回4,大小为一个32位无符号整数的字节。

#3


2  

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:

只需用标准十六进制表示法设置最大值,然后根据需要检查它。32位是8个十六进制字节,所以它是这样的:

let myMax: UInt32 = 0xFFFFFFFF

if myOtherNumber > myMax {
    // resolve problem
}

#4


0  

4.294.967.295 is the maximal value or in hexadecimal 0xFFFFFFFF

4.294.967.295是最大的值,或者是十六进制的0xFFFFFFFF。

#5


0  

The portable way:

便携式道:

std::numeric_limits<uint32_t>::max()

#6


0  

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

在C或c++中任何未签名的选项都是:

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:

这是有用的,因为它对所有人都有效,所以如果您从unsigned int更改为unsigned long,您不需要检查代码。你也会看到这个应用在很多琐碎的代码中:

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

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

缺点是它看起来很奇怪,给未签名的人分配一个负数!