简体   繁体   English

如何使用按位运算符返回0或1

[英]How to use bitwise operators to return a 0 or 1

My function takes in a 32 bit int and I need to return a 0 or 1 if that number has a 1 in any even position. 我的函数接受一个32位int,如果该数字在任何偶数位置都为1,则需要返回0或1。 I cant use any conditional statements I also can only access 8 bits at a time. 我不能使用任何条件语句,一次也只能访问8位。

Here is an example input: 10001000 01011101 00000000 11001110 这是一个示例输入:10001000 01011101 00000000 11001110

1) Shift the bits and and them with AA(10101010) and store each one in a variable. 1)用AA(10101010)移位位和它们,并将每个位存储在变量中。

int a = 10001000
int b = 1000
int c = 0
int d = 10001010

Now I need to return a 0 if there were no odd bits set and 1 if there were. 现在,如果没有设置奇数位,则需要返回0,如果有,则需要返回1。 As we can see there were. 正如我们所看到的。 So I need to combine these into one number and then use the !! 所以我需要将它们组合成一个数字,然后使用!! operator to return 0 or 1. This is where I am having trouble. 运算符返回0或1。这是我遇到问题的地方。

int valueToReturn = a | b | c | d;

Now I need to say: 现在我要说:

return !!valueTOReturn; 

It is not return the right value can anyone give me any insight??? 这不是回报正确的价值,任何人都可以给我任何见识吗???

I cannot use any condition statements like || 我不能使用||之类的任何条件语句 && &&

I figured it out. 我想到了。 Everything I said gives the right answer but I was grabbing the wrong value for one of my variables. 我说的一切都给出了正确的答案,但是我为我的一个变量获取了错误的值。 Thanks for all the help! 感谢您的所有帮助!

But, I don't know if I am understand correctly, if a have a 32 bit number, can have ALL bit OFF ONLY if is 0. 但是,我不知道我是否理解正确,如果a具有32位数字,则只有在为0时才可以将ALL位全部关闭。

if(input_number == 0 )
return 0;
else 
return 1;

If you can't use conditionals, you'll have to do this the ugly way: take the bit of each even-index bit and return the OR of them. 如果您不能使用条件语句,则必须以丑陋的方式进行:获取每个偶数索引位的位并返回它们的OR。 You can skip the mask. 您可以跳过面具。 This is kind of stupid though, and reeks of badly made homework. 不过,这是一种愚蠢的做法,而且功课不好。

Edited my answer to remove the erroneous first part. 编辑我的答案以删除错误的第一部分。

It appears that arnaud576875 is correct about the logical ! 看来arnaud576875关于逻辑是正确的! operator: http://www.gnu.org/s/gnu-c-manual/gnu-c-manual.html#The-Logical-Negation-Operator 运算符: http : //www.gnu.org/s/gnu-c-manual/gnu-c-manual.html#The-Logical-Negation-Operator

Until someone can find a C99 specification reference (GNU implementation might deviate from it), I guess I won't know for sure, but I suspect your version of C is as I described earlier. 直到有人找到C99规范参考(GNU实现可能会偏离它)之前,我想我不确定,但是我怀疑您的C版本是否如我先前所述。

First of all, you're not storing bits the way you're thinking. 首先,您没有按照自己的想法存储位。

int a = 10001000

is actually 10,001,000 (which in binary, b100110001001101001101000). 实际上是10,001,000(二进制,b100110001001101001101000)。

You say that the function takes in a 32-bit integer, so what you can do is extract each of the 8-bit portions like so: 您说该函数采用32位整数,因此您可以执行的操作是提取每个8位部分,如下所示:

unsigned char a, b, c, d;
a = (unsigned char)(input & 0xff);
b = (unsigned char)((input >> 8) & 0xff);
c = (unsigned char)((input >> 16) & 0xff);
d = (unsigned char)((input >> 24) & 0xff);

Now you can perform your masking/testing operation: 现在,您可以执行屏蔽/测试操作:

return (0xAA & a) | (0xAA & b) | (0xAA & c) | (0xAA & d);

As bdares says, it is just a lot of bitwise operations... evenbit_int evaluates to requested value. 正如bdares所说,这只是很多按位运算... evenbit_int计算evenbit_int请求值。

#define evenbit_byte(x) (((x) >> 1 | (x) >> 3 | (x) >> 5 | (x) >> 7) & 1)
#define evenbit_int(x) (evenbit_byte(x) | evenbit_byte(x >> 8) | evenbit_byte(x >> 16) | evenbit_byte(x >> 24))

or slightly more optimized 或更优化

byte evenbits(DWORD x)
{ 
  byte a = x >> 8;    
  byte b = x >> 16;    
  byte c = x >> 24;
  return (evenbit_byte(x) | evenbit_byte(a) | evenbit_byte(b) | evenbit_byte(c))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM