• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Help with two's complement

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

i have a question regarding two's complement in java. i was solving this question the other day

*****************************************************

class EBH005 {
public static void main (String[] s) {
byte b = 127; b <<= 2;
System.out.println(b);
}}

What is the result of attempting to compile and run the program?

a. Prints: -4
b. Prints: -3
c. Prints: -2
d. Prints: 0
e. Prints: 1
f. Prints: 127
g. Prints: 508
h. Run-time error
i. Compile-time error
j. None of the above

*************************************************************

the answer is a . Prints -4.

i cud understand the while conversion from 508 to byte , the left 3 bytes are left out. but with the remaining 1111 1100 how do u get to -4 .

i know there is the conversion from 0's to 1's and adding 1 , but i am little confused with that . can anyone kindly explain me the conversion and stuff.....

thx.

R0ss.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic