ios 如何将 UInt32 设置为其最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20070374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:27:00  来源:igfitidea点击:

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

iosobjective-cuint32

提问by Aran Mulholland

  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.

  1. UInt32 的最大值是多少?

  2. 有没有办法可以使用 sizeof 运算符来获取最大值(因为它是无符号的)?所以我的代码中不会出现 #defines 或幻数。

回答by CouchDeveloper

There's a macro UINT32_MAXdefined in stdint.hwhich you can use

UINT32_MAX定义了一个宏stdint.h,您可以在其中使用

#include <stdint.h>

uint32_t max = UINT32_MAX;


More about the relevant header <stdint.h>:

有关相关标题的更多信息<stdint.h>

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

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

回答by liamnichols

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 位无符号整数的字节大小。

回答by Ryan Dines

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
}

回答by Renaud

The portable way:

便携方式:

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

回答by Modass

4.294.967.295 is the maximal value or in hexadecimal 0xFFFFFFFF

4.294.967.295 是最大值或十六进制 0xFFFFFFFF

回答by Howard Lovatt

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 intto unsigned longyou don't need to go through your code. You will also see this used in a lot of bit fiddling code:

这很有用,因为它适用于所有人,因此如果您从 更改为unsigned intunsigned long则无需检查您的代码。您还将在许多位摆弄代码中看到它的使用:

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

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

缺点是它看起来很奇怪,将负数分配给无符号!