6

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

Trevin
  • 77
  • 1
  • 1
  • 3
  • 4
    Show us what you have first. I'm not doing your homework for you. – hookenz Aug 29 '11 at 01:08
  • Unfortunately, Matt, not everyone else agrees :( – KevinDTimm Aug 29 '11 at 01:16
  • @Matt H - I answered his question because it's "atomic". He had already taken the step to identifying the bit mask to use, so there was only one step to go. I find with bit-wise arithmetic, people only "get it" once they see a working answer. – Andrew Shepherd Aug 29 '11 at 03:12
  • 1
    Guys he said no conditional statements please read the question well before you answer :P – niko Aug 29 '11 at 05:09
  • @niko the conditionals are easy enough to factor out - the homework should remain somewhat of a challenge eh :-) – fvu Aug 29 '11 at 07:21

5 Answers5

5

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.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
  • Or simply "if (value & bit_mask)". Since if it's not true it'll return 0 and if it is, it'll be > 0. – hookenz Aug 29 '11 at 01:07
  • 2
    @Matt even if only a couple of bits are set but not all `value & bitmask` will evaluate to nonzero, so the test for equality with bitmask cannot be removed if you want to know that all bits in bitmask are set in value. – fvu Aug 29 '11 at 01:16
  • 1
    @fvu: He wants to know if every bit is CLEAR (set to 0), so `(value & bit_mask) == 0` is what he wants – Chris Dodd Aug 29 '11 at 02:30
  • @Chris Dodd - My mistake for not reading the question properly. I've updated my answer accordingly. – Andrew Shepherd Aug 29 '11 at 03:04
  • He also said he wasn't allowed to use "if" oddly enough. – hookenz Aug 29 '11 at 03:38
  • @fvi. oops... just realised my mistake. he wants to know if ALL even bits are zero. and we both did the negative case... – hookenz Aug 29 '11 at 03:43
  • `(value & bit_mask) == 0` is definitely the canonical way. It's potentially more efficient because computer hardware already makes testing a result for 0 / non-zero cheaper than testing for == to something else. Hopefully compilers will for example convert the `OR` method into a single x86 `test` instruction (set flags from `a & b` without destroying either input), instead of an actual `or` / `cmp` sequence. – Peter Cordes Nov 30 '17 at 06:31
3

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

fvu
  • 32,488
  • 6
  • 61
  • 79
3

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.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
1

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
vivek_jonam
  • 3,237
  • 8
  • 32
  • 44
0

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.

niko
  • 9,285
  • 27
  • 84
  • 131