开发者

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. I need to find out if every even bit is set 开发者_JAVA百科to 0 and return 0 if its true and 1 otherwise.

So far I am going to split my int using shifts into 4, 8 bit variables. 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.

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. Any ideas????


OK, so you've created a bit mask. (01010101)

 if ((value & bit_mask) == bit_mask)

then you know that each bit that was set in bit_mask is also set in 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)

There are two equally valid approaches: We make the bit mask the opposite (10101010)

Then use the OR operator:

if ((value | bit_mask) == bit_mask)

This checks that each bit that was zero in the bit_mask is zero in value.

The second approach is to make the bit mask the same (01010101) and use the AND operator:

if ((value & bit_mask) == 0)

This checks that each bit that is one in the bit_mask is zero in value.


There is no need to test every individual byte against a mask of 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.


EDIT: I got confused by the original question and followed OP in the negation thing - so basically this solves the reverse problem. Andrew Sheperd's edited solution starts back from the original problem and solves it in 1 step. Rudy Velthuis also offers an interesting approach.

If your bytevalue AND 01010101 == 01010101 all bits selected by the mask are 1 in the original byte value.

In sortof pseudo C:

unsigned char mask = 0x55;

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

or a slightly fancier xor based variation

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 ...


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜