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 each even bit is set to 0 and return 0 if its true and 1 otherwise.

So far, I'm going to split my int using shifts to 4, 8-bit variables. int a, b, c, d

Now I'm going to do it wrong, so now I will check if the bit is set to 1 instead of 0. To check whether it will be set to 1, I and them to 01010101.

Now I don’t know how to determine if every even bit is set to 1. I cannot use if / for / while loops or any conditional statements, and you need to use bitwise statements. Any ideas????

+6
source share
5 answers

OK, so you created a bitmask. (01010101)

if ((value & bit_mask) == bit_mask) 

then you know that every bit that was set in bit_mask is also set to value .


UPDATE: (after reading the question correctly)

You want to check if every second bit is set to 0. (Not set to 1, as my incorrect answer above shows)

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 every bit that was zero in 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 every bit that is one in bit_mask is zero in value .

+5
source

EDIT: I was confused by the original question and followed the OP in denying - so basically this solves the opposite problem. Edited solution Andrew Sheperd starts with the original problem and solves it in 1 step. Rudi Veltius also offers an interesting approach.

If your byte value 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 slightly more favorable xor-based variation

 unsigned char mask = 0x55; if (!((byteval & mask) ^ mask)) { printf ("all set"); } 

Btw, if very easy to get rid of the end result ...

+3
source

no need to check every single byte on the mask 0x55. Just "or" 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 "and" not more than 0, so it will return 1. If all the even bits are 0, 0 is returned.

+2
source

the logic is to use arithmetic operators, try the 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 true, otherwise the result is incorrect,

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 
+1
source

Just take an 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 a value of 0 in it even more than 0. and everything that 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 of them lead to zero, then the variable I has a zero value in it, otherwise some other value that can be considered True in c.

0
source

Source: https://habr.com/ru/post/896095/


All Articles