签署类型位运算移位运算符 [英] Bitwise shift operators on signed types

查看:193
本文介绍了签署类型位运算移位运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解位运算符的签署和无符号类型的行为。根据ISO / IEC的文件,以下是我的理解。

I am trying to understand the behavior of bitwise operators on signed and unsigned types. As per the ISO/IEC document, following are my understandings.


  • 的结果E1<< E2 ,是E1左移E2位的位置

  • The result of E1 << E2, is E1 left-shifted E2 bit positions

在帐户左移的空出的位置将由零填充。

The vacated bits on the account of left shift will be filled by zeros.

E1作为签署​​非负: E1&LT;&LT; E2 将导致以E1乘以2电源E2,如果值由结果类型重新presentable。

E1 as signed non-negative: E1 << E2 will result to E1 multiplied by 2 power of E2, if the value is representable by the result type.

Q1:什么关于签署底片

Q2:我无法理解什么是由模数减,在下面的上下文的意思。 如果E1有一个无符号类型,则结果的值是E1×2E2,降低模
三分之一以上的结果类型的最大值再presentable


  • 的结果E1&GT;&GT; E2 是E1右移E2位的位置。

  • The result of E1 >> E2 is E1 right-shifted E2 bit positions.

E1作为签署​​非负/无符号:结果的值是E1 / 2E2

E1 as signed non-negative/unsigned:The value of the result is the integral part of the quotient of E1 / 2E2

Q3:对于签署负整数我看到,有的书界定的空缺职位将充满 1 。请更多地讨论使用在签署负INT向右移位运算符。

Q3: For signed negative integers I see, some books defining that the vacant positions will be filled with 1.Please elaborate more on the use of right shift operator on signed negative int.

推荐答案

Q1:签署整数类型的负值左移操作符的行为是不确定的,因为是有符号整数类型的正值时的行为的结果 E1 * 2 ^ E2 不是再在类型presentable。

Q1: The behaviour of the left shift operator on negative values of signed integer types is undefined, as is the behaviour for positive values of signed integer types when the result E1 * 2^E2 is not representable in the type.

这是明确在第6.5.7条第4款和标准(n1570草案)5提到:

That is explicitly mentioned in section 6.5.7, paragraph 4 and 5 of the standard (n1570 draft):

4 E1 1所述的结果;&LT; E2 E1 左移 E2 位的位置;腾空位用零填充。如果 E1 有一个无符号类型,则结果的值为 E1×2 ^ E2 ,其模数减超过一人最大值再presentable的结果类型。如果 E1 有符号类型和非负值, E1×2 ^ E2 重新presentable的结果类型,然后就是结果值;否则,行为是不确定的。

4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2^E2 , reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2^E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

Q 2:还原模一比再在无符号整型$ P ​​$ psentable最大值意味着被移出左侧的位被简单地丢弃

Q2: The reduction modulo one more than the maximum value representable in the unsigned integer type means that the bits that are shifted out on the left are simply discarded.

在数学上,如果无符号类型的最大值为 2 ^ N - 1 (它是形式总是),移位的结果 E1 E2 位向左是该范围内的值 V 0〜 2 ^ N - 1 这样的区别

Mathematically, if the maximal value of the unsigned type is 2^n - 1 (and it is always of that form), the result of shifting E1 left by E2 bits is the value V in the range from 0 to 2^n - 1 such that the difference

(E1 * 2^E2 - V)

是整除 2 ^ N ,也就是说,它的划分,当你得到剩余 E1 * 2 ^ E2 2 ^ N

is divisible by 2^n, that is, it's the remainder you get when dividing E1 * 2^E2 by 2^n.

Q3:在转移符号整数类型的右负值的行为是实现定义。最常见的行为(至少在二的补码的机器)是一个算术移位,即,结果是商舍去(朝负无穷)。

Q3: The behaviour upon shifting right negative values of signed integer types is implementation-defined. The most common behaviour (at least on two's complement machines) is an arithmetic shift, that is, the result is the quotient rounded down (towards negative infinity).

5 E1&GT的结果;&GT; E2 E1 右移 E2 位的位置。如果 E1 有一个无符号的类型,或者 E1 有符号类型和非负值,则结果的值 E1 / 2 ^ E2 的商的组成部分。 如果 E1 有签署类型和负值,结果值是实现定义的。

5 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 / 2^E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.

这篇关于签署类型位运算移位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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