Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 227
Trevin Avatar asked Aug 29 '11 00:08

Trevin


People also ask

How do you know if all bits are 0?

The circuit for checking if all bits are zero is a NOR gate with all the bits of the vector as inputs. The output will be set to '1' only if all the input bits are interpreted as '0' , that's how a NOR gate works.

What is the bitwise operator used to set a particular bit value to 0 *?

The bitwise-AND operator compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

How do you check no is odd or even using bitwise operator?

The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise XOR Operation of the Number by 1 increment the value of the number by 1 if the number is even otherwise it decrements the value of the number by 1 if the value is odd.

How do you check for equality with bitwise operators?

Two numbers can be checked for equality even without using the == operator by employing bitwise operators. If you remember, the XOR operation would map to 0s for like bits. If two numbers are the same, they translate to the same bit sequence in binary.


3 Answers

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.

like image 76
Andrew Shepherd Avatar answered Nov 14 '22 22:11

Andrew Shepherd


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

like image 23
fvu Avatar answered Nov 14 '22 23:11

fvu


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.

like image 26
Rudy Velthuis Avatar answered Nov 14 '22 23:11

Rudy Velthuis