Java 中的位移

发布于 2024-09-26 17:09:21 字数 158 浏览 3 评论 0原文

我试图了解位移位是如何工作的。有人可以解释一下这行的含义吗:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

盛夏尉蓝 2024-10-03 17:09:21

分解:

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 is 00000000000000000000000000000001 in binary. As such, it will return 00000000000000000000000000000001 when n ends in a 1 (positive odd or negative even number) and 00000000000000000000000000000000 otherwise.

(n & 1) == 0 will hence be true if n is even (or negative odd) and false otherwise.

n >> = 1 is equivalent to n = 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.

你的往事 2024-10-03 17:09:21

n4,其二进制表示为:

00000000 00000000 00000000 00000100

(n&1) 按位,并将 n 与 <代码>1。
1 的二进制表示为:

00000000 00000000 00000000 00000001

按位与运算的结果是 0

00000000 00000000 00000000 00000100 = n
00000000 00000000 00000000 00000001 = 1
------------------------------------
00000000 00000000 00000000 00000000 = 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 be 4 which in binary is represented as:

00000000 00000000 00000000 00000100

(n&1) bitwise ands the n with 1.
1 has the binary representation of:

00000000 00000000 00000000 00000001

The result of the bitwise anding is 0:

00000000 00000000 00000000 00000100 = n
00000000 00000000 00000000 00000001 = 1
------------------------------------
00000000 00000000 00000000 00000000 = 0

so the condition of while is true.
Effectively (n&1) was used to extract the least significant bit of the n.

In the while loop you right shift(>>) n by 1. Right shifting a number by k is same as dividing the number by 2^k.

n which is now 00000000 00000000 00000000 00000100 on right shifting once becomes
00000000 00000000 00000000 00000010 which is 2.

Next we extract the LSB(least significant bit) of n again which is 0 and right shift again to give 00000000 00000000 00000000 0000001 which is 1.

Next we again extract LSB of n, which is now 1 and the loop breaks.

So effectively you keep dividing your number n by 2 till it becomes odd as odd numbers have their LSB set.

Also note that if n is 0 to start with you'll go into an infinite loop because no matter how many times you divide 0 by 2 you'll not get a odd number.

依 靠 2024-10-03 17:09:21

假设 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).

淡紫姑娘! 2024-10-03 17:09:21

我们假设等于 42 (只是因为):

int n = 42;

while ((n & 1) == 0) {
  n >>= 1;
}

迭代 0:

  • n = 42 (或 0000 0000 0000 0000 0000 0000 0010 1010< /code>)
  • n & 1 == 0true(因为 n&1 = 0 或 0000 0000 0000 0000 0000 0000 0000 0000

迭代 1:

  • n = 21(或0000 0000 0000 0000 0000 0000 0001 0101
  • n & 1 == 0false (因为 n & 1 == 10000 0000 0000 0000 0000 0000 0000 0001

它的作用:

基本上,只要 n 是偶数,您就循环将 n 除以 2:

  • n & 1 是一个简单的奇偶校验,
  • n >>= 1 将位向右移动,仅除以 2。

Let's assume equals 42 (just because):

int n = 42;

while ((n & 1) == 0) {
  n >>= 1;
}

Iteration 0:

  • n = 42 (or 0000 0000 0000 0000 0000 0000 0010 1010)
  • n & 1 == 0 is true (because n&1 = 0 or 0000 0000 0000 0000 0000 0000 0000 0000)

Iteration 1:

  • n = 21 (or 0000 0000 0000 0000 0000 0000 0001 0101)
  • n & 1 == 0 is false (because n & 1 == 1 or 0000 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 & 1 is a simple parity check,
  • n >>= 1 shifts the bits to the right, which just divides by 2.
人事已非 2024-10-03 17:09:21

例如,如果 n 是

n=    b11110000

那么

n&1=  b11110000 &
      b00000001 
      ---------
      b00000000

n>>=1 b11110000 >> 1
      ---------
      b01111000

n=    b01111000

如果循环继续它应该是

n=    b00001111

for example if n was

n=    b11110000

then

n&1=  b11110000 &
      b00000001 
      ---------
      b00000000

n>>=1 b11110000 >> 1
      ---------
      b01111000

n=    b01111000

if the loop continues it should be

n=    b00001111
别靠近我心 2024-10-03 17:09:21

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文