C使用按位运算符判断二进制数是否已将所有均数设置为0 [重复](C Using bitwise operators tell if binary number has all evens set to 0 [duplicate])

可能重复: 使用按位运算符查找是否将每个偶数位设置为0

另一个例子没有真正回答我的问题,所以这里是情况:

如果位序列中的所有均值都设置为0,则需要返回1,否则返回0。 - 我不能使用条件语句!

所以我有一个数字0x7f(01111111)

我可以通过面具0xAA(10101010)

这给了我:00101010

我只需要做一个0或1所以我!!(00101010),这将给我一个布尔值,但它返回1但我需要一个0所以我可以否定它或使用不同的掩码。

我一直跟着这个并且它让我疯狂,请帮助并记住这些运算符没有条件语句:

! 〜&^ | + << >>

Possible Duplicate: Find if every even bit is set to 0 using bitwise operators

The other example didnt really answer my question so here is the situation:

I need to return 1 if all evens in the bit sequence are set to 0 and return 0 otherwise. -I cant use conditional statements!

So I have a number 0x7f (01111111)

I can and by a mask of 0xAA(10101010)

that gives me: 00101010

I need to do only a 0 or 1 so I !!(00101010) and that will give me the boolean value for it but it returns a 1 but I need a 0 so I can negate it or use a different mask.

I keep going in circles with this and its driving me nuts please help and remember no conditional statements just these operators:

! ~ & ^ | + << >>

最满意答案

我错过了什么吗? 结果你得到00101010 ,所以不是所有的平均值都是0.在这种情况下你应该返回0,但是你!! ( 两次否定)结果。 为什么? 非零值被否定为假,然后被否定为真,这是1 ...正好与你需要的相反...

其他方式:使用0x15(00010101),所有均匀为0,ANDAD与0xAA给出0,否定给出true,再次否定给出false,结果为0 ...

Am I missing something? You get 00101010 as a result, so not all evens are 0. in that case you should return 0, but you !! (twice negate) the result. Why that? The non-zero value is negated to false, which is then negated to true, which is 1... Just the opposite of what you need...

Other way round: with 0x15 (00010101), all evens are 0, ANDing with 0xAA gives 0, negated gives true, again negated gives false, result is 0...

更多推荐