This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
First I would write out the three numbers in binary. 10 = 1010 12 = 1100 7 = 0111
Next ~1010 = 0101 So x = 0101 0101 ^ 1100 = 1001 So y = 1001 1001 & 0111 = 0001
I don't think there are any real tricks beyond that. From what I have heard there are only 1 or 2 of these questions on the test so taking a few minutes shouldn't be a problem (you only have to average 1 every 2 minutes).
Originally posted by Francisco Gonzalez: I know you ended up with the right answer, but is that always going to work ?
No, it won't always work. It did happen to work in this case, whether by luck or by clever foresight and optimization on the part of Steven.
The key is that final (y & 7) operation. "Anding" something by 7 effectively masks off all but the last three bits of the result, so even if you got bits 4 and up completely wrong, it wouldn't matter in this particular question.
Joined: Dec 29, 2004
Originally posted by Francisco Gonzalez: Thanks Steven for your response.
But there is something I don't understand, ~10 is really:
1111 1111 1111 1111 1111 1111 1111 1010
So in your calculation you are discarding all the 1's at the begining, I don't think you can't do that, or you can ? I know you ended up with the right answer, but is that always going to work ?
You're right, I saw the & 7 as a mask and ignored the higer bits (actually could have ignored the fourth one too). I should have said something as this only works with the mask there.
P.S. As a mental optimization you can generally keep track of the higher bits as a 0 or 1 as they are effected the same by most operations. Ned to be careful with that though and watch for right shifts. [ April 13, 2005: Message edited by: Steven Bell ]