# How to convert a Negative Binary Number to Decimal

Muhammad sibtain

Greenhorn

Posts: 17

posted 15 years ago

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

Please help me to do so

Regards

Sibtain

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

Please help me to do so

Regards

Sibtain

posted 15 years ago

The short answer is that we use 2s Complement representation for storing numbers, this allows us to store both positive and negative numbers.

For more info, see the relevant story from the Javaranch campsite

(do a search for "SIGNED" to find the specific bit)

Dave.

For more info, see the relevant story from the Javaranch campsite

(do a search for "SIGNED" to find the specific bit)

Dave.

Muhammad sibtain

Greenhorn

Posts: 17

posted 15 years ago

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

Regards

Sibtain

Peter Kristensson

Ranch Hand

Posts: 118

posted 15 years ago

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

http://www.javaranch.com/ubb/Forum33/HTML/003252.html

/Peter

John Lee

Ranch Hand

Posts: 2545

Anonymous

Ranch Hand

Posts: 18944

posted 14 years ago

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

Originally posted by Muhammad sibtain:

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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |