programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Bit twiddling

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
The following question is from K&B mock exam.

I know how all those bitwise operators work. But I'm having a hard time to manually perform the calculations, and for the exam, time is an enemy.

What's the best approach to attack a question like that ?

Thanks,
Francisco

Ranch Hand
Posts: 1071
• Number of slices to send:
Optional 'thank-you' note:
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

So System.out.println(1);

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

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
I sketch a table, with powers of 2 across the top and binary representations underneath...

This is great practice, but it's unlikely that the actual exam will have bitwise questions this involved. Even so, time shouldn't be that much of a concern.

Francisco Gonzalez
Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:

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 ?

Regards,
Francisco

Ranch Hand
Posts: 71
• Number of slices to send:
Optional 'thank-you' note:

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.

- Jeff

Steven Bell
Ranch Hand
Posts: 1071
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Francisco Gonzalez:

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 ?

Regards,
Francisco

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 ]

 Those are the largest trousers in the world! Especially when next to this ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising