请问C99标准保证unsigned int类型的二进制重新presentation? [英] Does the C99 standard guaranteed the binary representation of unsigned int?

查看:262
本文介绍了请问C99标准保证unsigned int类型的二进制重新presentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何填充位的值是unspeci网络版 45)有效
  (非陷阱)对象再有符号整数类型的presentation哪里
  符号位为零是对应的有效对象重新presentation
  无符号类型,并须重新present相同的值。

6.2.6.2/1 Integer types

The values of any padding bits are unspecified.45) A valid (non-trap) object representation of a signed integer type where the sign bit is zero is a valid object representation of the corresponding unsigned type, and shall represent the same value.

对于任何整数
  型,再presentation的对象,其中所有的位是零应是
  再在该类型的值为零的presentation。

在C99标准,所有的位均为零整数类型是保证重新present值 0 在各自的类型。然而,这是否保证底层的二进制值是我们希望它是什么?

In the C99 standard, an integer type where all the bits are zero is guaranteed to represent the value 0 in that respective type. However, does this guarantee that the underlying binary value is what we expect it to be?

例如:

unsigned x = 42;

我们通常会想到一台机器,此十进制 42 值存储在内存中二进制 101010 值。

We'd normally expect a machine to store this decimal 42 value in memory as the binary 101010 value.

然而,也有些古怪的机器架构存储相同的十进制 42 值作为二进制 011011 值(不一定对于实际原因,但仅仅是因为它可以)?

However, could some eccentric machine architecture store the same decimal 42 value as the binary 011011 value (not necessarily for a practical reason but simply because it can)?

如果是这样,请考虑以下code利用右移位操作:

If so, consider the following code utilizing a right shift operation:

unsigned y = x>>1;    /* 101010>>1 or 011011>>1 */

应该持有的十进制值 21 10101 二进制),或十进制值 13 01101 二进制)?

Would y hold the decimal value 21 (10101 in binary), or the decimal value 13 (01101 in binary)?

请问C99标准做出一个无符号整数类型的十进制重新presentation任何保证按位操作之后 - 例如是所有的机器架构保证等同于整数除法右移由 2

Does the C99 standard make any guarantee about the decimal representation of an unsigned integer type after a bitwise operation -- e.g. is a right shift guaranteed to be equivalent to an integer division by 2 on all machine architectures?

推荐答案

整数的再presentation未在标准中指定。结果
然而,行为>> << 根据位的含义界定,不他们的立场。结果
因此,>> 1 移动位重新presenting 4位再presenting 2,无论在哪里,这两位其实都是。

The representation of the integer isn't specified in the standard.
However, the behavior of >> and << is defined according to the meaning of bits, not their position.
So >> 1 moves the bit representing 4 to the bit representing 2, regardless of where these bits actually are.

引用href=\"http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf\" rel=\"nofollow\"> C99标准部分6.5

Quoting the C99 standard section 6.5.7:

E1 >> E2 E1 右移 E2 位位置的结果。如果 E1 有一个无符号类型
  或者,如果 E1 有符号类型和非负值,则结果的值的商的整数部分的 E1 / 2 E2 。如果 E1 有一个签署类型和负值,结果值为定义实现德网络。

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

这篇关于请问C99标准保证unsigned int类型的二进制重新presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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