负整数的位移? [英] Bit shifting of negative integer?

查看:111
本文介绍了负整数的位移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解负整数上的移位操作>>.

I am trying to understand the bit shift operation >> on a negative integer.

-2 >> 1 # -1
-3 >> 1 # -2
-5 >> 1 # -3
-7 >> 1 # -4

有人可以解释这是怎么做的吗?我知道这与Two的补码有关,但是我不能与移位运算有关.

Can someone explain how this is done? I know it is related to Two's complement, but I can't related that to the shifting operation.

推荐答案

此处

负整数的二进制补码:

Two's Complement binary for Negative Integers:

负数用前导号代替前导号 零.因此,如果您只使用8位二进制补码 数字,然后将模式从"00000000"到"01111111"视为 从0到127的整数,并保留"1xxxxxxx"用于写入 负数.使用该位写入负数-x (x-1)的所有位都被补全的模式(从1切换为 到0或0到1).所以-1是补码(1-1)=补码(0)= "11111111",并且-10是补码(10-1)=补码(9)= 补全("00001001")="11110110".这意味着负数 一直下降到-128("10000000").

Negative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then you treat patterns from "00000000" to "01111111" as the whole numbers from 0 to 127, and reserve "1xxxxxxx" for writing negative numbers. A negative number, -x, is written using the bit pattern for (x-1) with all of the bits complemented (switched from 1 to 0 or 0 to 1). So -1 is complement(1 - 1) = complement(0) = "11111111", and -10 is complement(10 - 1) = complement(9) = complement("00001001") = "11110110". This means that negative numbers go all the way down to -128 ("10000000").

当然,Python不使用8位数字.它曾经使用过 许多位本机是本机的,但是由于那是 不可移植,最近已切换为使用INFINITE号 位.因此,按位运算符将数字-5视为 写下"... 1111111111111111111010".

Of course, Python doesn't use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111011".

因此,移位运算符的解释:

So, the explanation for shift operator:

x >> y返回x,其位向右移y个位.这 与//将x乘以2 ** y相同.

x >> y Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.

为了理解上面的解释,您可以使用以下内容进行检查:

In order to understand the above explanation you can check it out with something like this:

def twos_comp(val, nbits):
    """Compute the 2's complement of int value val"""
    if val < 0:
        val = (1 << nbits) + val
    else:
        if (val & (1 << (nbits - 1))) != 0:
            # If sign bit is set.
            # compute negative value.
            val = val - (1 << nbits)
    return val

def foo(a,b):
    print("{0:b} >> {1:b} = {2:b} <==> {3:b} >> {4:b} = {5:b}".format(
        a,b,a>>b,
        twos_comp(a,8),b, twos_comp(a>>b,8)
    ))

foo(-2, 1)
foo(-3, 1)
foo(-5, 1)
foo(-7, 1)

哪个输出:

-10 >> 1 = -1 <==> 11111110 >> 1 = 11111111
-11 >> 1 = -10 <==> 11111101 >> 1 = 11111110
-101 >> 1 = -11 <==> 11111011 >> 1 = 11111101
-111 >> 1 = -100 <==> 11111001 >> 1 = 11111100

如您所见,数字的二进制补码将扩展符号.

As you can see, the two's complement of the number will extend the sign.

这篇关于负整数的位移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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