简体   繁体   English

正数和负数的按位运算符

[英]Bitwise Operator on positive and negative numbers

    -5 / 2 = -2

    -5 >> 1 = -3

I learn from my teacher that >>1 divides the number by 2. It works on positive number but it does not work on negative numbers. 我向老师学习>> 1将数字除以2.它对正数有效,但对负数不起作用。 Can someone explain to me?? 有人可以向我解释一下吗?

Thanks 谢谢

As BЈовић & mystical states, using bit shift operators on negative numbers is implementation defined. 作为BЈовић和神秘状态,在负数上使用位移算子是实现定义的。
The reason for this is C doesn't distinguish between logical and arithmetic bit shifting. 其原因是C不区分逻辑和算术位移。
(Arithmetic pads with the most significant bit, logical pads with 0's) (具有最高有效位的算术焊盘,带有0的逻辑焊盘)
for positive numbers this doesn't matter, for both arithmetic and logical bit shifts would keep the most significant bit as a 0: 对于正数,这无关紧要,因为算术和逻辑位移都会将最高位保持为0:
Arithmetic 5>>1 算术5 >> 1
0000 0000 0000 0101 = 5 0000 0000 0000 0101 = 5
to
0000 0000 0000 0010 = 2 0000 0000 0000 0010 = 2

Logical 5>>1 逻辑5 >> 1
0000 0000 0000 0101 = 5 0000 0000 0000 0101 = 5
to
0000 0000 0000 0010 = 2 0000 0000 0000 0010 = 2

however with a negative number (2's comp) 但是负数(2的比较)
Arithmetic -5>>1 算术-5 >> 1
1111 1111 1111 1011 = -5 1111 1111 1111 1011 = -5
to
1111 1111 1111 1101 = -3 1111 1111 1111 1101 = -3

Logical -5>>1 逻辑-5 >> 1
1111 1111 1111 1011 = -5 1111 1111 1111 1011 = -5
to
0111 1111 1111 1101 = 32,765 0111 1111 1111 1101 = 32,765

or at least, this is how i understand it 或者至少,这就是我理解它的方式

It works on positive number but it does not work on negative numbers. 它适用于正数,但不适用于负数。

Using shift operator on negative integer numbers is implementation defined. 在负整数上使用移位运算符是实现定义的。


[expr.shift]/3 tells this : [expr.shift] / 3告诉我:

The value of E1 >> E2 is E1 right-shifted E2 bit positions. E1 >> E2的值是E1右移E2位位置。 If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2E2 . 如果E1具有无符号类型或者E1具有有符号类型和非负值,则结果的值是E1 / 2E2的商的整数部分。 If E1 has a signed type and a negative value, the resulting value is implementation-defined. 如果E1具有带符号类型和负值,则结果值是实现定义的。

I learn from my teacher that >>1 divides the number by 2. 我向老师学习>> 1将数字除以2。

It doesn't divide the integer by two, but it performs (depending on the value) a logical or an arithmetic shift by one bit to the right. 它不会将整数除以2,但它会执行(取决于值) 逻辑算术移位一位向右。 It happens to be equal to a division by two under some circumstances. 在某些情况下,它恰好等于两分。

It works on positive number but it does not work on negative numbers. 它适用于正数,但不适用于负数。

It works in both cases, but the exact behavior is not mandated by the standard, but rather implementation-defined. 它适用于两种情况,但确切的行为不是标准规定的,而是实现定义的。 It usually divides by two and truncates the result towards negative infinity, in constrast to towards zero as a normal division would do. 它通常除以2并将结果截断为负无穷大,与正常除法相反,向右逼近零。

For reference: 以供参考:

First of all, 首先,
5 in binary is 0000 0000 0000 0101 but what about -5 ? 二进制5是0000 0000 0000 0101但是-5? Here it is : 这里是 :

  1. Change 1 to 0 and 0 to 1 ,then we get 1111 1111 1111 1010 将1更改为0,将0更改为1,然后我们得到1111 1111 1111 1010
  2. Then take that number + 1 , we get 1111 1111 1111 1011 然后取这个数字+ 1,我们得到1111 1111 1111 1011

Now we get: -5= 1111 1111 1111 1011 ( it's in 2's complement form) 现在我们得到:-5 = 1111 1111 1111 1011(以2的补码形式)
So here is how to calculate -5>>1 : 那么这里是如何计算-5 >> 1:

  1. Move each bit of -5 from left to right ( >> ) we get 111 1111 1111 1101 (only 15 bits left) 将每个位-5从左向右移动(>>)我们得到111 1111 1111 1101(仅剩15位)
  2. Because -5 is negative number, so we have to fill '1' to the first bit to make it become 16 bits Then we get 1111 1111 1111 1101 ( it's still in 2's complement form) 因为-5是负数,所以我们必须将'1'填充到第一位使其变为16位然后我们得到1111 1111 1111 1101(它仍然是2的补码形式)
  3. Now convert it to a normal binary form by change 0 to 1 , 1 to 0 (except the first bit because it defines negative number in 2's complement), then plus '1' . 现在将其转换为正常的二进制形式,方法是将0更改为1,将1转换为0(第一位除外,因为它定义了2的补码中的负数),然后再加上'1'。 so we get 1000 0000 0000 0011 = -3 所以我们得到1000 0000 0000 0011 = -3

I think answer is correct. 我认为答案是正确的。 As '/' (division) operator generate quotient (result of division). 作为'/'(除法)运算符生成商(除法的结果)。

In your problem : 在你的问题中:

-5/2 = -3(quotient) and 1(remainder ). 

So this is ok with both positive and negative number. 所以这对正数和负数都没问题。

positive Number: 正数

5/2 = 2(quotient) and 1(remainder ). 

So it is fine with positive Number. 所以没有正数。

NOTE 注意

Remainder never be a negative number. 剩余永远不会是负数。 It is always positive Number. 它总是正数。

I guess the answer to -5>>1 = -3. 我猜答案为-5 >> 1 = -3。 In case of a positive number, say 5, division by 2 gives 2.5 rounding off to the nearest smallest integer ie 2 在正数的情况下,比如5,除以2得到2.5舍入到最接近的最小整数,即2

But when we consider a negative number, -5, division by 2 gives -2.5. 但是当我们考虑负数-5时,除以2给出-2.5。 Its rounding off to the nearest integer gives -3. 它四舍五入到最接近的整数给出-3。

在c中,右移位运算符保留符号位。因此,在保持符号位的情况下右移位,产生负数,这是两个补码形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM