简体   繁体   English

使用按位运算符查找是否每个偶数位都设置为0

[英]Find if every even bit is set to 0 using bitwise operators

I have a 32 bit int I can only access it 8 bits at a time. 我有一个32位的int我一次只能访问它8位。 I need to find out if every even bit is set to 0 and return 0 if its true and 1 otherwise. 我需要找出每个偶数位是否设置为0,如果为真,则返回0,否则返回1。

So far I am going to split my int using shifts into 4, 8 bit variables. 到目前为止,我将使用转换为4位,8位变量来拆分我的int。 int a, b, c, d int a,b,c,d

Now I am going to not them so now I will test if the bit is set to 1 instead of 0. To test if its set to 1 I will and them by 01010101. 现在我要不要他们所以现在我将测试该位是否设置为1而不是0.要测试它是否设置为1我将和01010101。

Now I dont know how to tell if every even bit is set to 1. I cannot use if/for/while loops or any conditional statements and need to use bitwise operators. 现在我不知道如何判断每个偶数位是否设置为1.我不能使用if / for / while循环或任何条件语句并且需要使用按位运算符。 Any ideas???? 有任何想法吗????

OK, so you've created a bit mask. 好的,所以你创建了一个位掩码。 (01010101) (01010101)

 if ((value & bit_mask) == bit_mask)

then you know that each bit that was set in bit_mask is also set in value . 那么你就知道在bit_mask中设置的每个位也都设置了value


UPDATE: (after reading the question properly) 更新:(正确阅读问题后)

You want to check if every second bit is set to 0. (Not set to 1, as my incorrect answer above checks for) 您想要检查每个第二位是否设置为0.(未设置为1,因为我上面的错误答案检查)

There are two equally valid approaches: We make the bit mask the opposite (10101010) 有两种同样有效的方法:我们使位掩码相反(10101010)

Then use the OR operator: 然后使用OR运算符:

 if ((value | bit_mask) == bit_mask) 

This checks that each bit that was zero in the bit_mask is zero in value . 这将检查bit_mask中为零的每个位的value是否为零。

The second approach is to make the bit mask the same (01010101) and use the AND operator: 第二种方法是使位掩码相同(01010101)并使用AND运算符:

 if ((value & bit_mask) == 0) 

This checks that each bit that is one in the bit_mask is zero in value . 这将检查bit_mask中的每个位的value是否为零。

EDIT: I got confused by the original question and followed OP in the negation thing - so basically this solves the reverse problem. 编辑:我对原始问题感到困惑,并在否定事情中遵循OP - 所以基本上这解决了相反的问题。 Andrew Sheperd's edited solution starts back from the original problem and solves it in 1 step. Andrew Sheperd编辑的解决方案从最初的问题开始,并在一步中解决它。 Rudy Velthuis also offers an interesting approach. Rudy Velthuis也提供了一种有趣的方法。

If your bytevalue AND 01010101 == 01010101 all bits selected by the mask are 1 in the original byte value. 如果您的字节值AND 01010101 == 01010101掩码选择的所有位在原始字节值中为1。

In sortof pseudo C: 在sortof伪C:

unsigned char mask = 0x55;

if ((byteval & mask) == mask) {
    printf ("all set");
}

or a slightly fancier xor based variation 或者基于xor的略微变化的变体

unsigned char mask = 0x55;

if (!((byteval & mask) ^ mask)) {
    printf ("all set");
}

Btw, the if is very easy to get rid of for the final result ... 顺便说一句, if很容易摆脱最后的结果......

There is no need to test every individual byte against a mask of 0x55. 无需针对0x55的掩码测试每个字节。 Just "or" the bytes together and test the result against the mask: 只需将“或”字节放在一起,然后根据掩码测试结果:

return ((a | b | c | d) & 0x55 != 0);

Any even bit set to 1 will make the result of the "and" not be 0 anymore, so it will return 1. If all even bits are 0, then 0 is returned. 任何设置为1的偶数位都会使“和”的结果不再为0,因此它将返回1.如果所有偶数位都为0,则返回0。

the logic is to make use of arithmetic operators,, try following steps,, 逻辑是利用算术运算符,尝试以下步骤,

     1. AND the each result with 01010101
     2. then atlast AND all the results,, now if the resulting decimal value is 
      85(decimal(01010101))

then the result is correct else the result is wrong,, 那么结果是正确的,否则结果是错误的,

try this sample code,, 试试这个示例代码,,

//assume a,b,c,d has the four parts of the bits,,
//do the following for each variable
Ar=a & 01010101
.
.
.
.
Dr=d & 01010101

//now AND all r2 for each variable..
r=Ar & Br & Cr & Dr

//now check the decimal equivalent of r and decide true if it is 85

Just take some integer variable and store the value in it. 只需取一些整数变量并将值存储在其中。

i= ( a & 0x55 ) + (b & 0x55 ) + ( c & 0x55 ) + (d & 0x55 )

If all the even bits are set to zero,The variable i has 0 value in it else greater than 0.and anything which is not equal to zero it is true in c. 如果所有偶数位都设置为零,则变量i的值为0,否则大于0.并且任何不等于零的值在c中为真。

say

 a = 10101010 & 0x55 ( 01010101) which returns zero,masking all odd bits to zero

similarly 同样

 b & 0x55 and c & 0x55 and d & 0x55 

and if all they result to zero then variable i has zero value in it,else some other value that may be considered as True in c. 如果它们全部归零,则变量i的值为零,否则在c中可能被视为True的其他值。

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

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