my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes negative numbers-2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "negative numbers-2" Watch "negative numbers-2" New topic
Author

negative numbers-2

Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
If I am giving the following number in Binary,and have to convert it to decimal,then what is the method for it?
11000111
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
1[B] 1 0 0 0 1 1 1
7 6 5 4 3 2 1 0
1*2^7+1*2^6+0*2^5+0*2^4+0*2^3+1*2^2+1*2^1+1*2^0=
128+64+0+0+0+4+2+1=199
Best regards,
Jamal Hasanov
www.j-think.com
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
1 1 0 0 0 1[B] [B]1 1
7 6 5 4 3 2 1 0
1*2^7+1*2^6+0*2^5+0*2^4+0*2^3+1*2^2+1*2^1+1*2^0=
128+64+0+0+0+4+2+1=199
Best regards,
Jamal Hasanov
www.j-think.com
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Please ignore previous posts - Some errors
1 1 0 0 0 1 1 1
7 6 5 4 3 2 1 0
Easy method :
If first digit is 1 (and size is maximal, for example: 8 in byte,16 in word,etc)
-(1*2^7+1*2^6+0*2^5+0*2^4+0*2^3+1*2^2+1*2^1+1*2^0)=
128-64-0-0-0-4-2-1=-57
Best regards,
Jamal Hasanov
www.j-think.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: negative numbers-2