• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

bitwise operations

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody,
I am a newcomer, I am preparing for SCJP, I have bought Kathy's book and I am doing some exercises other than that i found in the book...
this evening I found the following question on http://www.javaprepare.com/quests/operat_q.html
question number 11
What gets printed when the following program is compiled and run. Select the one correct answer.

well, the possible answers are

and the right answer is H ??? why
here is the process I do in my mind, please tell me where is my fault!
1. a byte is 8 bit
2. a byte with value 3 has this bit rapresentation 00000011
3. now the complement operator ~ inverts all the bits, so that each 1 bit becomes 0, and each 0 bit becomes 1. If this is right I should obtain:
11111100 which is 252... am i right? or I missing something?
P.S.: please forgive my bad english... java is not the only language I have to learn
Ciao, Gabriele
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gabriele,
One thing you overlooked is 11111100 has 1 on the MSB (sign bit) indicating it is a negative
number which is -4.
 
Gabriele Gigliotti
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Chi Lin
thanks for your fast reply...
You are right I forgotten the meaning of the MSB but I still miss the answer
a byte with value -4 has maybe this bit rapresentation?
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for 2's compliment, the formula to get the negative is

-x = ~x + 1 ,
let x be 4, the representation for -4 is then
~0000100 -> 11111011
+ 1 -> 00000001
--------------------------------
11111100,
PS :I have some problem to get the alignemt
right, hope it is OK this time .
[ January 31, 2003: Message edited by: chi Lin ]
 
Gabriele Gigliotti
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chi Lin,
I really appreciate your lessons about operations on bits...
 
reply
    Bookmark Topic Watch Topic
  • New Topic