如果使用 C 中的位运算,整数中的任何位等于 1,则返回 1
我已经思考这个问题几个小时了。这里是:
编写一个表达式,如果给定整数“x”有任何位等于 1,则返回 1。否则返回 0。
我知道我本质上只是想弄清楚 x == 0 是否存在,因为这是唯一没有 1 位的 int,但我无法找出解决方案。您不得使用传统的控制结构。您可以使用按位运算符、加法、减法和位移位。建议?
I've been thinking about this problem for hours. Here it is:
Write an expression that returns 1 if a given integer "x" has any bits equal to 1. return 0 otherwise.
I understand that I'm essentially just trying to figure out if x == 0 because that is the only int that has no 1 bits, but I can't figure out a solution. You may not use traditional control structures. You may use bitwise operators, addition, subtraction, and bit shifts. Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这是我能想到的最好的:
其中 BITS = 32 表示 32 位整数,即
BITS = sizeof(int) * CHAR_BIT;
这是一个测试程序:
Here's the best I could come up with:
where BITS = 32 for 32 bit ints, i.e.
BITS = sizeof(int) * CHAR_BIT;
Here's a test program:
使用 !!x 会给你正确的答案。因为 !0 = 1 且 !(任何非零数)= 0。
Using !!x will give you the right answer. Since !0 = 1 and !(any nonzero number) = 0.
对于 32 位值,以下内容适用于所有位模式。
For a 32-bit value, the following will work for all bit-patterns.
单独屏蔽每个位,将它们全部下移到 lsb 位置,或者将它们一起移动。
Mask each of the bits individually, shift them all down to the lsb position, and or them together.
您可以将
int
转换为bool
。但我怀疑这就是你作业的目的;-)You could just cast your
int
to abool
. But I doubt that's the purpose of your homework ;-)对于 32 位整数
For 32 bit integers
0 || number - 仅当数字为 0 时才返回 0,如果数字为 0 以外的任何其他数字,则返回 1。由于没有任何位为 1 的数字将等于 0,因此我们需要用 0 来检查它。
0 || number - this will return 0 only if the number is 0 and will return 1 if the number is any other number than 0. Since a number without any bit as 1 will be equal to 0, we need to check it with 0.
未经测试,这是我想到的第一件事:
如果循环 n 后的
e == 16
为 0。untested, that's the first thing that came to my mind:
if
e == 16
after the loop n is 0.0 和任何数字的按位与必须等于 0,但唯一万无一失的测试是使用 0xFFFF 或设置每个位。要设置所有位,您应该有一个有符号 int,并将其分配为 -1。然后,您将得到一个所有位都设置为 1 的 int,无论大小如何。
所以我的答案是按位与-1
Bitwise AND with 0 and any number must equal zero, but the only foolproof test would be with 0xFFFF, or every bit being set. To get all bits set, you should have a signed int, and assign it -1. You will then have an int with all bits set to 1, regardless of size.
So my answer would be to bitwise AND it with -1
!(x&&~x)&&x 怎么样?
这似乎有效,但我不确定何时会发生溢出。
How about !(x&&~x)&&x ?
It seems work, but I'm not sure when overflow happens.
我相信这是最简单的方法。
x 中唯一没有 1 的情况是当所有位都为 0 或 x == 0 时。因此 0|0 -> 0 否则 0|x ->非零。
I believe this is the simplest way.
The only time your x will not have a 1 in it is when all bits are 0, or x == 0. So 0|0 -> 0 else 0|x -> non zero.
在 C 语言中,除 ZERO 之外的任何值(无论是正数还是负数)都被视为 TRUE。并且应该有一个条件来检查您的问题的解决方案是否返回零或一(或非零)。因此,这个答案完全符合您的要求。这仅使用按位运算符。
当“x”中的任何位都不为 1 时,该行返回 0;当“x”中的任何位为 1 时,该行返回非零(某种意义上为 TRUE)。
In C language, any value other than ZERO (either positive or negative) is treated as TRUE. And there should be a condition to check either your question's solution returns a ZERO or ONE (or other than ZERO). Therefore this answer is perfectly as per your requirement. This uses only bit-wise operators.
This line returns ZERO when neither of any bit in "x" is 1, and returns Non-Zero (TRUE in a sense) when any of the bit is 1 in "x".