什么是C和C ++标准说关于位整数水平再presentation和操纵? [英] What do the C and C++ standards say about bit-level integer representation and manipulation?

查看:75
本文介绍了什么是C和C ++标准说关于位整数水平再presentation和操纵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道C和C ++标准没有规定对数字的特定再presentation(可能是二进制补码,登录和幅度等)。但我不知道不够好标准(而找不到如果它说)知道,如果有任何特殊的限制/保证/保留重$ P $有位工作时提出psentations。特别是:


  1. 如果在整数类型的所有位均为零,确实整数作为一个整体再present零?

  2. 如果一个整数类型的任何位为1,不整数作为一个整体再present非零? (如果这是一个是,那么一些重新presentations像标志和幅度将受到限制附加)

  3. 有没有保证的方式来检查是否有任何位没有设置?

  4. 有没有保证的方式来检查是否有任何位被设置? (#3,#4种取决于#1和#2,因为我知道如何设置,例如第5位(见#5)某些变量 X ,我想检查变量来看看它的第5位为1,我想知道,如果如果(X安培; Y)将工作(因为据我所知,这依赖于重presentation的价值,不在于是否也并不是说一点实际上是1或0))

  5. 有没有保证的方式来设置最左边的和/或最右边的位? (至少不是采取简单的方式字符ç所有位真(由 C = C设置|〜ç ),并在 C = C<<(CHAR_BIT - 1)设置高位和 C = C ^(C<&LT 1)的低位,假设我不是做任何假设我should't是,考虑到这些问题)

  6. 如果答案#1是不何以遍历位整数类型,并检查是否每个人是1还是0?

我想我的整体的问题是:有没有什么限制/保证/保留重$ P $由C和C制成psentations对位和整数++标准,尽管一个整数的再度presentation不是强制性的(如果C和C ++标准,在这方面有所区别,什么是它们的区别)?

我想到了这些问题,而做功课这需要我做一些位操作(注意,这些是无法从我的家庭作业的问题,这些都是更抽象)。

编辑:至于什么我称之为位,我的意思是价值形成位,我不包括填充位


解决方案

  

(1)如果在整数类型的所有位均为零,确实整数作为一个整体再present零?


是的,包括全零的位模式总是重新presents 0:


  

整数类型的重新presentations应由使用纯二进制记数系统的定义值。 49 [§3.9.1/ 7]


  
  

49 对于整数A位置重新presentation使用二进制数字0和1,其中,所述值重新由连续位都是添加剂psented $ P $,以1开始,并且由2逐次积分功率乘以,除非是具有最高位置位。




  

(2)如果一个整数类型的任何位为1,不整数作为一个整体再present非零? (如果这是一个是,那么一些重新presentations像标志和幅度将受到限制附加)


没有。事实上,签署幅度特别允许的:


  

[示例:的本国际标准允许2的补码,1的补和整型签署幅度再presentations。的端起
  例如
的]的 [§3.9.1/ 7]




  

(3)有没有保证的方式来检查是否有任何位没有设置?


我的认为的这个问题的答案是没有,如果你考虑符号类型。它相当于平等全部为一的位模式,如果你有办法,生产全部为一的位模式有符号数这是唯一可能的测试。对于一个无符号数这个重新presentation得到保证,而且从符号转换为签署是不确定的,如果数字是unre presentable:


  

如果目标类型是签署的值不变,如果它可以被重新$ P $在目标类型psented(和比特字段宽度);否则,该值是实现定义的。 [§4.7/ 3]




  

(4)有没有保证的方式来检查是否有任何位被设置?


我不这么认为,因为震级签署允许-0将比较等于-0。但它应该是可能的无符号数。



  

(5)有没有保证的方式来设置最左边的和/或最右边的位?


同样,我相信答案是是无符号数,但不签号码。调整是未定义负符号数:


  

否则,如果 E1 已在签署的类型和非负值,E1×2 E2 重新presentable结果类型,然后就是得到的值;否则,其行为是不确定的。 [§5.8/ 2]


I know the C and C++ standards don't dictate a particular representation for numbers (could be two's complement, sign-and-magnitude, etc.). But I don't know the standards well enough (and couldn't find if it's stated) to know if there are any particular restrictions/guarantees/reserved representations made when working with bits. Particularly:

  1. If all the bits in an integer type are zero, does the integer as whole represent zero?
  2. If any bit in an integer type is one, does the integer as a whole represent non-zero? (if this is a "yes" then some representations like sign-and-magnitude would be additionally restricted)
  3. Is there a guaranteed way to check if any bit is not set?
  4. Is there a guaranteed way to check if any bit is set? (#3 and #4 kind of depend on #1 and #2, because I know how to set, for example the 5th bit (see #5) in some variable x, and I'd like to check a variable y to see if it's 5th bit is 1, I would like to know if if (x & y) will work (because as I understand, this relies on the value of the representation and not whether nor not that bit is actually 1 or 0))
  5. Is there a guaranteed way to set the left-most and/or right-most bits? (At least a simpler way than taking a char c with all bits true (set by c = c | ~c) and doing c = c << (CHAR_BIT - 1) for setting the high-bit and c = c ^ (c << 1) for the low-bit, assuming I'm not making any assumptions I should't be, given these questions)
  6. If the answer to #1 is "no" how could one iterate over the bits in an integer type and check if each one was a 1 or a 0?

I guess my overall question is: are there any restrictions/guarantees/reserved representations made by the C and C++ standards regarding bits and integers, despite the fact that an integer's representation is not mandated (and if the C and C++ standards differ in this regard, what's their difference)?

I came up with these questions while doing my homework which required me to do some bit manipulating (note these aren't questions from my homework, these are much more "abstract").

Edit: As to what I refer to as "bits," I mean "value forming" bits and am not including "padding" bits.

解决方案

(1) If all the bits in an integer type are zero, does the integer as whole represent zero?

Yes, the bit pattern consisting of all zeroes always represents 0:

The representations of integral types shall define values by use of a pure binary numeration system.49 [§3.9.1/7]

49 A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position.


(2) If any bit in an integer type is one, does the integer as a whole represent non-zero? (if this is a "yes" then some representations like sign-and-magnitude would be additionally restricted)

No. In fact, signed magnitude is specifically allowed:

[ Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. —end example ] [§3.9.1/7]


(3) Is there a guaranteed way to check if any bit is not set?

I believe the answer to this is "no," if you consider signed types. It is equivalent to equality testing with a bit pattern of all ones, which is only possible if you have a way to produce a signed number with bit pattern of all ones. For an unsigned number this representation is guaranteed, but casting from unsigned to signed is undefined if the number is unrepresentable:

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined. [§4.7/3]


(4) Is there a guaranteed way to check if any bit is set?

I don't think so, because signed magnitude is allowed—0 would compare equal to −0. But it should be possible with unsigned numbers.


(5) Is there a guaranteed way to set the left-most and/or right-most bits?

Again, I believe the answer is "yes" for unsigned numbers, but "no" for signed numbers. Shifts are undefined for negative signed numbers:

Otherwise, if E1 has a signed type and non-negative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined. [§5.8/2]

这篇关于什么是C和C ++标准说关于位整数水平再presentation和操纵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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