• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question from dan's exam:On operators

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class D {
public static void main (String args[]) {
int i1 = ~1;
int i2 = -1;
int i3 = -2;
System.out.print(Integer.toHexString(i1) + ",");
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3));
}
}

What is the result of attempting to compile and run the above program?
a. Prints: ffffffff,ffffffff,ffffffff
b. Prints: ffffffff,ffffffff,fffffffe
c. Prints: ffffffff,fffffffe,ffffffff
d. Prints: ffffffff,fffffffe,fffffffe
e. Prints: fffffffe,ffffffff,ffffffff
f. Prints: fffffffe,ffffffff,fffffffe
g. Prints: fffffffe,fffffffe,ffffffff
h. Prints: fffffffe,fffffffe,fffffffe
i. Runtime error
j. Compiler error
k. None of the above
I am somehow just not getting any of the above values. The answer is:f
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should get it.
What is your answer ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running the program gives the answer of f.
What does your program give?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chiran,
This is really just a question about the two's complement representation of integral values. A good tutorial on the subject is the Cat and Mouse Games with Bits.
If you need to review hexadecimal then the following base conversions tutorial might be helpful.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Running the program gives the answer of f.
What does your program give?


Barry,
I'm sure that Chiran obtains the results specified by answer f. Furthermore, I suspect that Chiran is either American or lives here in the United States and sometimes uses the statement "I don't get it" in place of the statement "This appears to be nonsense."
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dan. I know, I'm an absolute beast sometimes
I actually did paste the program into BlueJ (plug) to test it, just in case, and it gave answer f. I was just trying to get Chiran to elaborate on his misunderstanding...
Cheers ,
-Barry
 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
Thanks a lot for the great tutorials. I am confused about -2 though. Wouldn't -2 be
1111 1101
and therfore the answer will be fffffffd
instead of fffffffe
Please clarify.
Thanks,
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chiran, the trick to remember is "flip the bits and add one". So, using 8 bits, 2 is 0000 0010.
"Flipping" the bits gives: 1111 1101
Adding one: 1111 1110, in hexadecimal that is FE.
So, in terms of an int: -2 is 0xfffffffe.
Cheers , Barry
reply
    Bookmark Topic Watch Topic
  • New Topic