为什么这有效? (在c++中查找奇数)
for (unsigned int i = 1; i <= 100; i++) {
if (i & 0x00000001) {
std::cout << i<<",";
}
}
为什么(以及如何): if( i & 0x00000001 )
计算出奇数?
for (unsigned int i = 1; i <= 100; i++) {
if (i & 0x00000001) {
std::cout << i<<",";
}
}
why does (and how): if( i & 0x00000001 )
figure out the odd number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
0x00000001
在二进制中是1
,尽管它是以十六进制(base-16)表示法编写的。这就是0x
部分。&
是按位“AND”运算符,用于进行二进制数字(位)操作。我& 1
将 i 的所有二进制数字转换为零,除了最后一位。将生成的 1 位数字转换为布尔值非常简单,以便通过
if
语句进行计算。下图显示了 i 的最后 16 位二进制数字,以及它们发生了什么。
0x00000001
is1
in binary, although it's written in hexadecimal (base-16) notation. That's the0x
part.&
is the bit-wise 'AND' operator, which is used to do binary digit (bit) manipulations.i & 1
converts all of the binary digits of i to zero, except for the last one.It's straightforward to convert the resulting 1-bit number to a boolean, for evaluation by the
if
statement.The following chart shows the last 16 binary digits of i, and what happens to them.
它使用按位“与”运算符来屏蔽除最后一位之外的所有内容。如果最后一位是 1,则该数字是奇数。这解释够了吗?
It's using a bitwise "and" operator to mask off all but the last bit. If the last bit is a 1, the number is odd. Is that enough explanation?
当我们查看以 10 为基数的数字时,很容易判断一个数字是否能被 10 整除:它的最后一个位置有一个 0。上面的代码也查看最后一个位置的数字,但以 2 为基数。如果它非零,则该数字不能被 2 整除。
When we look at numbers in base 10, it's easy to tell if a number is divisible by 10: it has a 0 in the last position. The code above looks at the digit in the last position as well, but in base 2. If it's non-zero, the number is not divisible by 2.
它掩盖了最后一点。如果您查看数字的二进制表示形式中的每个位置(...、256、128、64、32、16、8、4、2 和 1),您会发现只有一个位置是奇数。无论位是设置还是清除,所有其余位置都具有偶数值(零为偶数)。偶数相加总是得到偶数。只有最后一个的位置决定了数字的奇偶性。
i & &0x00000001
部分只是隔离了最后一个的位置。It masks off the last bit. If you look at each place in the binary representation of a number (..., 256, 128, 64, 32, 16, 8, 4, 2, and 1), you'll notice that only the one's place is odd. All the rest of the places have an even value whether the bits are set or cleared (zero being even). And adding even numbers will always give an even number. Only that last one's place determines the parity of the number. The
i & &0x00000001
part just isolates that last one's place.奇数是 (2*n+1) 形式的所有数字,其中 n 是任意整数 (-2,-1,0,1...)。因此,要找到奇数,您必须查看正在使用的整数上是否有“+1”。当存储为无符号整数时,数字可以表示为 2 的幂和:2^0 + 2^1 +2^2 + 2^4 等。无符号整数的二进制版本看起来像真值图各种各样的:对于二进制数中每一个有“1”的地方,将这个数字加上2的幂。因此,如果从无符号整数的二进制表示形式中最右边的数字开始并向左移动,则每个数字代表 2 的幂。如果该数字是 1,那么当您到达二进制数的末尾时,将 2 的幂添加到运行总和中。
因此: 10001110b 可以通过对 2 的幂求和来转换为整数:
最右边:2^1 + 2^2 + 2^3 + 2^7 :最左边 = 141
诀窍是最右边的数字代表 2^0。这始终是 1。所有其他数字代表偶数。因此,就奇数而言,您必须找到“+1”。这对应于最右边的数字。所有其他数字表示“2*n”形式的数字。因此,要确定此格式的数字(无符号整数)是否为奇数,您只需查看最右边的位是否为“1”。
对无符号整数执行的运算是逻辑 AND 运算。任何与 0 进行 AND 运算的结果都是 0,1 与 1 进行 AND 运算的结果都是 1。因此,该运算将导致无符号整数表示中除表示 2^0 的二进制数字(即 1)之外的所有二进制数字均为 0。因此,如果是奇数,则无符号整数二进制表示形式将归结为 0x00000001,如果是偶数,则归结为 0x00000000。
注意:当我写0x00000000时,“0x”表示它是十六进制格式。每个“0”代表四位。所以 0x00 是十六进制的 00000000b
以二进制形式写出,对无符号整数进行 AND 运算后可能得到的无符号整数二进制表示为:
00000000000000000000000000000000b == 0
和
00000000000000000000000000000001b==1
Odd numbers are all numbers of the form (2*n+1) where n is any integer (-2,-1,0,1....). So to find an odd number you have to see if the integer you're working with has that '+1' on it. When stored as an unsigned integer, a number can be expressed as the sum of powers of 2: 2^0 + 2^1 +2^2 + 2^4, etc. The binary version of the unsigned integer looks like a truth map of sorts: for every place there's a '1' in the binary number, add that power of two to the number. So, if you start at the right most digit in the binary representation of the unsigned integer and move left, each digit represents a power of two. If the digit is 1 then you add that power of two to the running sum and when you reach the end of the binary number.
Thus: 10001110b can be converted into an integer by summing the powers of two:
Rightmost: 2^1 + 2^2 + 2^3 + 2^7 :Leftmost = 141
The trick is that the rightmost digit represents 2^0. This is always 1. All the other digits represent even numbers. So, in terms of odd numbers you have to find the '+1'. That corresponds to the rightmost digit. All the other digits represent numbers of the form '2*n'. Therefore, to determine if a number of this format (unsigned integer) is odd you need only see if the right most bit is '1'.
The operation performed on the unsigned integer is a logical AND operation. Anything AND'd with 0 is 0, and 1 AND'd with 1 is 1. So the operation will cause all binary digits in the unsigned integer representation to be 0 except for the binary digit representing 2^0 (which is 1). So the unsigned integer binary representation will boil down to 0x00000001 if it's odd and 0x00000000 if it's even.
Note: When I write 0x00000000, the '0x' means that it's hexadecimal format. Each '0' represents four bits. So 0x00 is hexadecimal for 00000000b
Written out in binary, the resulting possible unsigned integer binary representations after AND'ing the unsigned integer are:
00000000000000000000000000000000b == 0
and
00000000000000000000000000000001b == 1
您正在进行按位比较。如果 bit0 AND bit0 都为 1,则答案的 bit0 = 1。
看到 &0x00000001 为 1,任何具有该位的数字都是奇数。
所以,如果你执行按位 AND
You are doing bitwise comparison. The if bit0 AND bit0 are both 1, then the answer's bit0 = 1.
Seeing that &0x00000001 is 1, any numbers with this bit are odd.
So, if you do a bitwise AND
在我说以下内容之前,我首先要说的是,我几乎总是使用位测试来确定 int 是奇数还是偶数。
但是,严格来说,您应该使用
(i % 2)
或((i % 2) != 0)
来确定i
是奇怪的。无论负数的表示如何,这都将起作用,而在补码机器(-3&amp; 0x01)上将返回零(错误结果),即使它显然是奇数。我意识到现在使用二进制补码以外的东西来表示负数的机器非常非常罕见,但现在的编译器确实会普遍编译
(i % 2)
来进行位测试。请记住,我通常不会遵循自己的建议,因此这可能表明了该建议的真正价值。Before I say the following, I'll first say that I pretty much always use the bit test to determine whether an int is odd or even.
But, strictly speaking, you should use
(i % 2)
or((i % 2) != 0)
to determine isi
is odd. This will work regardless of the represenation of negative numbers, while on a one's complement machine (-3 & 0x01) will return zero (a false result) even though clearly it's odd.I realize that machiens that use something other than two's complement to represent negative number are very,very rare nowadays, but it's also true that compilers nowadays will universally compile
(i % 2)
to a bit test anyway. And remember, I usually don't follow my own advice here, so that's might be an indication of the true value of this suggestion.按位 AND 运算符的每一位都遵循此真值表:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
由于计算机使用以 2 为基数的数字,并且每位代表两个值的幂 (1,2,4,8,16 ..),
最低有效位代表唯一的奇数,无论数字有多大或多小。 value 是,并且无论它是有符号还是无符号。由于仅当两个操作数都设置了该位时才设置结果位,因此当且仅当数字为奇数时结果为 true。
例子:
0b11101011 =
(1 * 2^0) + (1 * 2^1) + (0 * 2^2) + (1 * 2^3) +
(0 * 2^4) + (1 * 2^5) + (1 * 2^6) + (1 * 2^7) =
1 + 2 + 0 + 8 + 0 + 32 + 64 + 128 = 235
如果没有设置最低有效位,则该值将为 234,因此是偶数。
The bitwise AND operator follows this truth table for every bit:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
Since computers work with numbers in base 2 and each bit represents a power of two value (1,2,4,8,16 ..),
the least significant bit represents the only odd number, regardless how big or small the value is, and regardless whether it is signed or unsigned. Since the resulting bit is only set if both operands have that bit set, the result is true if and only if the number is odd.
Example:
0b11101011 =
(1 * 2^0) + (1 * 2^1) + (0 * 2^2) + (1 * 2^3) +
(0 * 2^4) + (1 * 2^5) + (1 * 2^6) + (1 * 2^7) =
1 + 2 + 0 + 8 + 0 + 32 + 64 + 128 = 235
Without the least significant bit set, the value would be 234 and hence an even number.
例如,我们如何制作一个等价的二进制
8 4 2 1
0 0 0 0 =
0 0 0 0 1 = 1
0 0 1 0 = 2
0 0 1 1 = 3
所以你可以看到任何奇数。 LSB 始终设置,与您检查的相同:)
我希望我的答案很清楚
For Example how we make a Binary Equivalent
8 4 2 1
0 0 0 0 = 0
0 0 0 1 = 1
0 0 1 0 = 2
0 0 1 1 = 3
So you can see for any odd no. LSB is always set , Same you check :)
I hope my answer is clear