programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to convert a Negative Binary Number to Decimal

Greenhorn
Posts: 17
I am not able to convert a binary number to decimal by applying the same technique that we normally use to convert a positive binary e.g.
if i want to convert 00000111 the i can convert it by this
(0*2^7)+(0*2^6)+(0*2^5)+(0*2^4)+(0*2^3)+(1*2^2)+(1*2^1)+(1*2^0)
0 + 0 + 0 + 0 + 0 +( 1*4 )+( 1*2 )+( 1*1 )
0 + 0 + 0 + 0 + 0 + 4 + 2 + 1
the total of these = 7
00000111 = 7

and
11111000 = -7
if i use the same rule it is giving some thing else in decimal
Regards
Sibtain

David O'Meara
Rancher
Posts: 13459
The short answer is that we use 2s Complement representation for storing numbers, this allows us to store both positive and negative numbers.
(do a search for "SIGNED" to find the specific bit)
Dave.

Greenhorn
Posts: 17
Thanks for your quick response David but, My problem is not this that how java put a negative number in memory i now it perform 2s complement and then place it in memory. My problem is this if i wannt to convert that negative binaray number manually what formula should i apply for it??
Regards
Sibtain

Peter Kristensson
Ranch Hand
Posts: 118
There's a thread on this in the java in general forum, hope it helps
http://www.javaranch.com/ubb/Forum33/HTML/003252.html
/Peter

John Lee
Ranch Hand
Posts: 2545
Change negative into absolute value:
Two steps:
1) Deduct 1 form original binary;
2) Switch all byte of binary number;
Now you get the absolute value of the nagative. Follow normal procedure.

Anonymous
Ranch Hand
Posts: 18944
I am not able to convert a binary number to decimal by applying the same technique that we normally use to convert a positive binary e.g.
if i want to convert 00000111 the i can convert it by this
(0*2^7)+(0*2^6)+(0*2^5)+(0*2^4)+(0*2^3)+(1*2^2)+(1*2^1)+(1*2^0)
0 + 0 + 0 + 0 + 0 +( 1*4 )+( 1*2 )+( 1*1 )
0 + 0 + 0 + 0 + 0 + 4 + 2 + 1
the total of these = 7
00000111 = 7

and
11111000 = -7
if i use the same rule it is giving some thing else in decimal

11111000 would equal -7 in a one-complement binary notation. In two-complement notation (which is a bit more popular) -7 would be represented by 1111001. Here goes; assume we're talking 8 bits in total where the leftmost bit is the sign bit. Jot down the following numbers, from left to right: 128, 64, 32, 16, 8, 4, 2, 1.
For any positive number (the leftmost bit is 0), say, 00000111, take the corresponding numbers you've just jotted down, where a bit equals 1, so here you get 4, 2, 1. Add these numbers: 4+2+1= 7.
Now for a negative number (the leftmost bit equals 1), say, 11111001, take the corresponding numbers you've just jotted down, where a bit equals 0, here you get 4, 2. Add these numbers: 4+2=6 and add one (that's the two-complement trick), so the result equals 7, but the number is negative, so the final result equals -7.
For one-complement representation, the 'add one to the result' step can be omitted.
kind regards,
Jos

 It is sorta covered in the JavaRanch Style Guide.