Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个整数,我必须将它向右移动几次。
x = 27 = 11011 x>>1= 13 = 1101 x>>2= 6 = 110 x>>3= 3 = 11
我想获取已删除的值位值。我必须得到: 1, 1, 0
我怎样才能得到删除的值
(x & 1)为您提供最低有效位的值。你应该在换班之前计算一下。
(x & 1)
对于最低有效位,您可以x & 1在每次移位之前使用。如果您想随时获取自定义位而不更改值,可以使用以下方法。
x & 1
private static int GetNthBit(int x, int n) { return (x >> n) & 1; }
在删除它之前只需测试第一个位
int b = x & 1;
请参阅有关 & 运算符的 MSDN 参考