[英]Return 1 if any bits in an integer equal 1 using bit operations in C
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. 如果给定的整数“x”具有等于1的任何位,则写入一个返回1的表达式。否则返回0。
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. 我明白我实际上只是想弄清楚x == 0,因为那是唯一没有1位的int,但我无法找到解决方案。 You may not use traditional control structures. 您可能不会使用传统的控制结构。 You may use bitwise operators, addition, subtraction, and bit shifts. 您可以使用按位运算符,加法,减法和位移。 Suggestions? 建议?
Here's the best I could come up with: 这是我能想到的最好的:
y = (((-x) | x) >> (BITS - 1)) & 1;
where BITS = 32 for 32 bit ints, ie BITS = sizeof(int) * CHAR_BIT;
其中BITS = 32表示32位整数,即BITS = sizeof(int) * CHAR_BIT;
Here's a test program: 这是一个测试程序:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char *argv[])
{
const int BITS = sizeof(int) * CHAR_BIT;
if (argc == 2)
{
int x = atoi(argv[1]);
int y = (((-x) | x) >> (BITS - 1)) & 1;
printf("%d -> %d\n", x, y);
}
return 0;
}
Using !!x will give you the right answer. 使用!! x会给你正确的答案。 Since !0 = 1 and !(any nonzero number) = 0. 由于!0 = 1和!(任何非零数字)= 0。
分别屏蔽每个位,将它们全部向下移动到lsb位置,或者将它们一起移位。
对于32位值,以下内容适用于所有位模式。
return (a | -a) >> 31;
How about !(x&&~x)&&x ? 怎么样!(x &&~x)&& x?
#include <stdio.h>
void main(){
int x;
scanf("%d",&x);
printf("%d\n",(!(x&&~x)&&x));
}
It seems work, but I'm not sure when overflow happens. 这似乎有用,但我不确定何时发生溢出。
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. number - 仅当数字为0时返回0,如果数字是0以外的任何其他数字,则返回1.因为没有任何位的数字将等于0,我们需要用0检查它。
You could just cast your int
to a bool
. 你可以把你的int
转换为bool
。 But I doubt that's the purpose of your homework ;-) 但我怀疑这是你作业的目的;-)
int any_bits_to_one(unsigned int n) {
int result = 0, i;
for (i=0; !result && i < sizeof(unsigned int) * 8; i++)
result |= (n & (1<<i)) ? 1 : 0;
return result;
}
For 32 bit integers 对于32位整数
int return_not_zero(int v)
{
r=v;
r=(r&0xFFFF) | (r>>16);
r=(r&0xFF) | (r>>8);
r=(r&0x0F) | (r>>4);
r=(r&0x03) | (r>>2);
r=(r&0x01) | (r>>1);
return r;
}
untested, that's the first thing that came to my mind: 未经测试,这是我想到的第一件事:
while(n & pow(2, e) == 0 && e++ <= 16) ; // 16 or 32
if e == 16
after the loop n is 0. 如果在循环n为0之后e == 16
。
Bitwise AND with 0 and any number must equal zero, but the only foolproof test would be with 0xFFFF, or every bit being set. 按位与0和任何数字必须等于零,但唯一的万无一失的测试将是0xFFFF,或每个位被设置。 To get all bits set, you should have a signed int, and assign it -1. 要设置所有位,您应该有一个signed int,并将其指定为-1。 You will then have an int with all bits set to 1, regardless of size. 然后,无论大小如何,您都将拥有一个将所有位设置为1的int。
So my answer would be to bitwise AND it with -1 所以我的答案是按位和它-1
I believe this is the simplest way. 我相信这是最简单的方法。
return !!(0|x);
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. 你的x唯一没有1的时候是所有位都是0,或者x == 0.所以0 | 0 - > 0否0 | x - >非零。
In C language, any value other than ZERO (either positive or negative) is treated as TRUE. 在C语言中,除零之外的任何值(正或负)都被视为TRUE。 And there should be a condition to check either your question's solution returns a ZERO or ONE (or other than ZERO). 并且应该有一个条件来检查您的问题的解决方案返回ZERO或ONE(或ZERO除外)。 Therefore this answer is perfectly as per your requirement. 因此,这个答案完全符合您的要求。 This uses only bit-wise operators. 这仅使用逐位运算符。
return (x & 0xFFFF);
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". 当“x”中的任何位都不为1时,该行返回ZERO,并且当“x”中的任何位为1时,返回非零(在某种意义上为TRUE)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.