Java 中的位移
我试图了解位移位是如何工作的。有人可以解释一下这行的含义吗:
while ((n&1)==0) n >>= 1;
其中n
是一个整数,并给我一个执行移位时n
的例子。
I'm trying to understand how bit shift works. Can someone please explain the meaning of this line:
while ((n&1)==0) n >>= 1;
where n
is an integer and give me an example of a n
when the shift is executed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
分解:
n & 1
将在 n 和 1 之间进行二进制比较,即二进制的00000000000000000000000000000001
。因此,当 n 以 1(正奇数或负偶数)结尾时,它将返回00000000000000000000000000000001
,否则返回00000000000000000000000000000000
。因此,如果 n 为偶数(或负奇数),则
(n & 1) == 0
将为 true,否则为 false。n>> = 1
相当于n = n >>> 1.
.因此,它将所有位向右移动,这大致相当于除以二(向下舍入)。例如,如果 n 开始为 12,那么在二进制中它将是 1100。在一个循环之后它将是 110 (6),在另一循环之后它将是 11 (3),然后循环将停止。
如果n为0,那么下一次循环后它仍然是0,并且循环将是无限的。
Breaking it down:
n & 1
will do a binary comparison between n, and 1 which is00000000000000000000000000000001
in binary. As such, it will return00000000000000000000000000000001
when n ends in a 1 (positive odd or negative even number) and00000000000000000000000000000000
otherwise.(n & 1) == 0
will hence be true if n is even (or negative odd) and false otherwise.n >> = 1
is equivalent ton = n >> 1
. As such it shifts all bits to the right, which is roughly equivalent to a division by two (rounding down).If e.g. n started as 12 then in binary it would be 1100. After one loop it will be 110 (6), after another it will be 11 (3) and then the loop will stop.
If n is 0 then after the next loop it will still be 0, and the loop will be infinite.
令
n
为4
,其二进制表示为:(n&1)
按位,并将n
与 <代码>1。1
的二进制表示为:按位与运算的结果是
0
:因此 while 的条件为 true。
实际上,
(n&1)
用于提取n
的最低有效位。在 while 循环中,您将 (
>>
)n
右移1
。将数字右移k
与将数字除以2^k
相同。n
现在右移后变为00000000 00000000 00000000 00000100
00000000 00000000 00000000 00000010
,即2
。接下来,我们再次提取
n
的LSB(最低有效位),即0
,并再次右移得到00000000 00000000 00000000 0000001
,即<代码>1。接下来,我们再次提取 n 的 LSB,现在是
1
并且循环中断。因此,您可以有效地将数字
n
除以2
,直到它变为奇数,因为奇数已设置其LSB。另请注意,如果
n
一开始就是0
,您将进入无限循环,因为无论您将0
除以 < 多少次code>2 你不会得到奇数。Lets
n
be4
which in binary is represented as:(n&1)
bitwise ands then
with1
.1
has the binary representation of:The result of the bitwise anding is
0
:so the condition of while is true.
Effectively
(n&1)
was used to extract the least significant bit of then
.In the while loop you right shift(
>>
)n
by1
. Right shifting a number byk
is same as dividing the number by2^k
.n
which is now00000000 00000000 00000000 00000100
on right shifting once becomes00000000 00000000 00000000 00000010
which is2
.Next we extract the LSB(least significant bit) of
n
again which is0
and right shift again to give00000000 00000000 00000000 0000001
which is1
.Next we again extract LSB of n, which is now
1
and the loop breaks.So effectively you keep dividing your number
n
by2
till it becomes odd as odd numbers have their LSB set.Also note that if
n
is0
to start with you'll go into an infinite loop because no matter how many times you divide0
by2
you'll not get a odd number.假设 n = 12。其位数为 1100 (1*8 + 1*4 + 0*2 + 0*1 = 12)。
第一次通过循环 n & 1 == 0 因为 1100 的最后一位数字是 0,当你将它与 1 相与时,你会得到 0。所以 n >>= 1 将导致 n 从 1100 (12) 变为 110 (6)。您可能会注意到,右移与除以 2 具有相同的效果。
最后一位仍然为零,因此 n & 1仍然是0,所以会再右移一次。 n>>=1 将导致它向右再移动一位数字,将 n 从 110 (6) 更改为 11 (3)。
现在你可以看到最后一位是 1,所以 n & 1 将变为 1,导致 while 循环停止执行。循环的目的似乎是将数字向右移动,直到找到第一个打开的位(直到结果为奇数)。
Assume n = 12. The bits for this would be 1100 (1*8 + 1*4 + 0*2 + 0*1 = 12).
The first time through the loop n & 1 == 0 because the last digit of 1100 is 0 and when you AND that with 1, you get 0. So n >>= 1 will cause n to change from 1100 (12) to 110 (6). As you may notice, shifting right has the same effect as dividing by 2.
The last bit is still zero, so n & 1 will still be 0, so it will shift right one more time. n>>=1 will cause it to shift one more digit to the right changing n from 110 (6) to 11 (3).
Now you can see the last bit is 1, so n & 1 will be 1, causing the while loop to stop executing. The purpose of the loop appears to be to shift the number to the right until it finds the first turned-on bit (until the result is odd).
我们假设等于
42
(只是因为):迭代 0:
n = 42
(或0000 0000 0000 0000 0000 0000 0010 1010< /code>)
n & 1 == 0
为true
(因为 n&1 = 0 或0000 0000 0000 0000 0000 0000 0000 0000
)迭代 1:
n = 21
(或0000 0000 0000 0000 0000 0000 0001 0101
)n & 1 == 0
是false
(因为n & 1 == 1
或0000 0000 0000 0000 0000 0000 0000 0001
)它的作用:
基本上,只要 n 是偶数,您就循环将 n 除以 2:
Let's assume equals
42
(just because):Iteration 0:
n = 42
(or0000 0000 0000 0000 0000 0000 0010 1010
)n & 1 == 0
istrue
(because n&1 = 0 or0000 0000 0000 0000 0000 0000 0000 0000
)Iteration 1:
n = 21
(or0000 0000 0000 0000 0000 0000 0001 0101
)n & 1 == 0
isfalse
(becausen & 1 == 1
or0000 0000 0000 0000 0000 0000 0000 0001
)What it does:
Basically, you loop divides n by 2 as long as n is an even number:
例如,如果 n 是
那么
如果循环继续它应该是
for example if n was
then
if the loop continues it should be
n & 1实际上是按位与运算。这里,n 的位模式将与 1 的位模式进行 ANDED。Who 的结果将与 0 进行比较。如果是,则 n 右移 1 次。您可以将右移一位视为除以 2,依此类推。
n & 1 is actually a bitwise AND operataion. Here the bit pattern of n would be ANDED against the bit pattern of 1. Who's result will be compared against zero. If yes then n is right shifted 1 times. You can take the one right shift as division by 2 and so on.