UINT_MAX是否将所有位都设置为1? [英] Does UINT_MAX have all bits set to 1?

查看:162
本文介绍了UINT_MAX是否将所有位都设置为1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题以前被问过,但我仍然很困惑.

This question is asked before but I am still confused.

我知道

unsigned int a = -1;

将是UINT_MAX.但这不是因为设置了-1的所有位. C11说

would be UINT_MAX. But it is not because all bits of -1 is set. C11 says

如果新类型是无符号的,则通过重复加或 比新类型可以表示的最大值多减去一个 直到该值在新类型的范围内

if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type

所以可以说UINT_MAX是100(我知道它应该大于2 ^ 16-1,但现在暂时忽略它)

So lets say UINT_MAX is 100 (I know it should be greater then 2^16-1 but lets ignore this for now)

unsigned int a = -1; // will be
unsigned int a = -1 + UINT_MAX + 1; // 100 = UINT_MAX  

标准只说UINT_MAX >= 2^16-1.但是它说在任何地方都应该是2 ^ n-1吗?

Standard only says UINT_MAX >= 2^16-1. But does it say anywhere it should be 2^n-1?

答案在C ++中也不同吗?

Also is answer different in C++?

推荐答案

在C中,无符号整数的最大值必须采用 1 的形式:2 N -1.

In C, the maximum value for an unsigned integer must be in the form1: 2N - 1.

因此,值UINT_MAX的所有 value 位将设置为1.可能有 padding 位,其值未指定.

Thus all value bits of the value UINT_MAX will be set to 1. There may be padding bits, whose values are unspecified.

1 (引自:ISO/IEC 9899:201x 6.2.6.2整数类型1)
对于除无符号字符以外的无符号整数类型,对象表示的比特应分为两组:值比特和填充比特(后者无须再填充).如果有 N 个值位,则每个位应表示1和2 N -1 之间的2的幂.类型应能够使用纯二进制表示形式表示从0到2 N -1的值;这将被称为值表示.未指定任何填充位的值.

1 (Quoted from: ISO/IEC 9899:201x 6.2.6.2 Integer types 1)
For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N−1 , so that objects of that type shall be capable of representing values from 0 to 2N−1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.

这篇关于UINT_MAX是否将所有位都设置为1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆