简体   繁体   English

如何从c#中的按位右移中删除位

[英]How to get the bit removed from the bitwise right shift in c#

I have a int number And I have to shift it to the right a couple of times. 我有一个int号而且我必须将它向右移几次。

x   = 27 = 11011
x>>1= 13 = 1101
x>>2=  6 = 110
x>>3=  3 = 11

I would like to get the value bit value that has been removed. 我想获取已被删除的值位值。 I would have to get: 1, 1, 0 我必须得到:1,1,0

How can I get the removed value 如何获取删除的值

(x & 1) gives you the value of the least significant bit. (x & 1)给出最低有效位的值。 You should calculate it before you shift. 你应该转移之前计算它。

For least significant bit you can use x & 1 before each shifting. 对于最低有效位,您可以在每次换档之前使用x & 1 If you want to get a custom bit at any time without changing the value, you can use below method. 如果要在不更改值的情况下随时获取自定义位,可以使用以下方法。

    private static int GetNthBit(int x, int n)
    {
        return (x >> n) & 1;
    }

Just test the first bit before removing it 在删除之前测试第一位

int b = x & 1;

See MSDN reference on & operator 请参阅&operator上的MSDN参考

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

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