1

Possible Duplicate:
Find if every even bit is set to 0 using bitwise operators

The other example didnt really answer my question so here is the situation:

I need to return 1 if all evens in the bit sequence are set to 0 and return 0 otherwise. -I cant use conditional statements!

So I have a number 0x7f (01111111)

I can and by a mask of 0xAA(10101010)

that gives me: 00101010

I need to do only a 0 or 1 so I !!(00101010) and that will give me the boolean value for it but it returns a 1 but I need a 0 so I can negate it or use a different mask.

I keep going in circles with this and its driving me nuts please help and remember no conditional statements just these operators:

! ~ & ^ | + << >>

Community
  • 1
  • 1
Muru
  • 41
  • 5
  • Duplicates: [Find if every even bit is set to 0 using bitwise operators](http://stackoverflow.com/questions/7225112/find-if-every-even-bit-is-set-to-0-using-bitwise-operators), [How to use bitwise operators to return a 0 or 1](http://stackoverflow.com/questions/7214973/how-to-use-bitwise-operators-to-return-a-0-or-1) and [Access to nth bit without a conditional statement](http://stackoverflow.com/questions/7211069/access-to-nth-bit-without-a-conditional-statement) – Paul R Aug 31 '11 at 13:20
  • 1
    I don't agree that its a duplicate since he wanted every other bit and may have been looking for a one-line way of achieving that. Its not necessarily the same as looking at every bit (at least not to someone who is inexperienced.) – John Humphreys Aug 31 '11 at 13:24
  • What happens if you use `!` instead of `!!` ? – Paul R Aug 31 '11 at 13:24
  • The answer is simply '!(x&0xAA)' – Heath Hunnicutt Aug 31 '11 at 14:34

2 Answers2

0

(~(2^1 & yournumber)) & (~(2^3 & yournumber)), etc all the way down would work. Not sure if its the fastest way though (~(2^1 & yournumber)) & (~(2^3 & yournumber)), etc all the way down would work. Not sure if its the fastest way though - and you could make a function to cycle through and make it go on for a configurable number of bits.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • There is also another constraint and that is I can only use 12 operators total and the number is actually 32 bits but I was trying to simplify it and then I will expand it but thank you. – Muru Aug 31 '11 at 13:34
  • @Muru: what you can take from w00te's answer is you can check individual bits (`^` is the exponent operation, not XOR; `2 ^ x` can be implemented with a `<<`, though you can also implement a solution using `>>`). The next thing to ask yourself is: how do I take checking individual bits and use that to check all the bits I'm interested in? There is, however, a faster way... – outis Aug 31 '11 at 18:38
0

Am I missing something? You get 00101010 as a result, so not all evens are 0. in that case you should return 0, but you !! (twice negate) the result. Why that? The non-zero value is negated to false, which is then negated to true, which is 1... Just the opposite of what you need...

Other way round: with 0x15 (00010101), all evens are 0, ANDing with 0xAA gives 0, negated gives true, again negated gives false, result is 0...

king_nak
  • 11,313
  • 33
  • 58
  • In the assignment, evens start at the LSB. The !! operator from what I understand turns a value into an int 0 being true and anything else being false. So !!4 would be 1 and !!0 would be 0. – Muru Aug 31 '11 at 14:01
  • Ok, starting with LSB, the mask is 0x55. ANDing with that will give you only the even positions, that are not 0. If this value is != 0, at least 1 even bit is non-zero. In that case, you should return 0. As the AND-result is non-zero (==true), it's negation is false, which is int 0 – king_nak Aug 31 '11 at 14:13
  • Ok so 00000111 anded with 01010101 gives 00000101 but I dont know how to say what you are saying without using conditionals. – Muru Aug 31 '11 at 14:16
  • `!(0x07 & 0x55)` gives you `0`. Not all even bits are 0 (#6 & #8 are 1). – king_nak Aug 31 '11 at 14:18
  • So you are saying that since it gives 00000101 and then noted, just once, it reverses that boolean value to 0. – Muru Aug 31 '11 at 14:23
  • Yes, exactly. The `!!` can be used to get 0 to 0 and non-zero to 1. This is the opposite of what you want, so you have to either negate once more (`!!!`), or simply drop one negation... – king_nak Aug 31 '11 at 14:25
  • 1
    @Muru, I hope you realize, there is no such thing as the '!!' operator -- what you've written is two applications of the '!' operator. In comparison, '>' is greater-than while '>>' is right-shift; but there is no special meaning for '!!', in fact you could put space or parens in there: '! !x' or '!(!x)'. – Heath Hunnicutt Aug 31 '11 at 14:31
  • @Heath that really helps out. I was thinking exactly how you thought I was so thank you!. Just to clarify since this is an issue with me, if I have a number 0101 and I do this: !(0101) it returns 0 since its a number greater than 1 and if the number is 0000 and I do this: !(0000) it returns 0 because its a 0. If you can write a simple example like that for me, it would be appreciated very much. – Muru Aug 31 '11 at 14:38
  • @Muru -- You have almost got it. !(0101) returns 0 because 0101 is non-zero. The logical invert changes non-zero to zero and zero to one (which is, of course, one of the non-zero members of the set on which ! is defined) so that !1 = 0 and therefore: !!0 = 0 and for all other numbers where x!=0, then !!x = 1. Don't use the concept "greater than zero" because !(-1) or !(-3) is also 0. – Heath Hunnicutt Aug 31 '11 at 16:17