Tomas Elpacio

Greenhorn

Posts: 5

posted 15 years ago

Does anyone know where I can find info about doing decimal to binary conversions and vice-versa for negative numbers. I understand that the high-order bit determines if it is negative or not, but I have not seen any illustrations/diagrams of this, which would help me with the learning process. Thanks in advance.

Peter Kristensson

Ranch Hand

Posts: 118

posted 15 years ago

It's quite simple really

When the first (highest order) bit is 1, then the number is negative; you have to 2-complement it (invert all the digits and add 1). Then you've got the original number, convert it to decimal and stick a minus-sign in front of it and you're done!

Let me know if this isn't enough

/Peter

When the first (highest order) bit is 1, then the number is negative; you have to 2-complement it (invert all the digits and add 1). Then you've got the original number, convert it to decimal and stick a minus-sign in front of it and you're done!

Let me know if this isn't enough

/Peter

Tomas Elpacio

Greenhorn

Posts: 5

Peter Kristensson

Ranch Hand

Posts: 118

posted 15 years ago

Ok, it's been a while since I did this by hand but here goes:

-7

first, lets get the binary number for 7(dec):

0000 0000 0000 0000 0000 0000 0000 0111

no we have to 2-complement it,

this is done by first 1-complementing (inverting) it:

1111 1111 1111 1111 1111 1111 1111 1000

then, we add 1 to the result:

1111 1111 1111 1111 1111 1111 1111 1001

this represents the decimal figure -7

so you see, the first (highest order) digit is 1, the number is negative.

just to make sure, I'll transform it back for ya, so you see that it works both ways:

number:

1111 1111 1111 1111 1111 1111 1111 1001

we notice that this is has a leding 1, i.e. it's negative.

we 1-complement it (invert):

0000 0000 0000 0000 0000 0000 0000 0110

add 1 to the result:

0000 0000 0000 0000 0000 0000 0000 0111

and the number 0111(bin) is equal to 7(dec)

just stick your minus-sign to it and you're done!

/Peter

-7

first, lets get the binary number for 7(dec):

0000 0000 0000 0000 0000 0000 0000 0111

no we have to 2-complement it,

this is done by first 1-complementing (inverting) it:

1111 1111 1111 1111 1111 1111 1111 1000

then, we add 1 to the result:

1111 1111 1111 1111 1111 1111 1111 1001

this represents the decimal figure -7

so you see, the first (highest order) digit is 1, the number is negative.

just to make sure, I'll transform it back for ya, so you see that it works both ways:

number:

1111 1111 1111 1111 1111 1111 1111 1001

we notice that this is has a leding 1, i.e. it's negative.

we 1-complement it (invert):

0000 0000 0000 0000 0000 0000 0000 0110

add 1 to the result:

0000 0000 0000 0000 0000 0000 0000 0111

and the number 0111(bin) is equal to 7(dec)

just stick your minus-sign to it and you're done!

/Peter

Terence Doyle

Ranch Hand

Posts: 328

posted 15 years ago

HI,

I've seen a number of posts with doubts on how to calculate 2's complement numbers.

The following is an explanation of how the calculation is made from a telecommunications course I'm doing:

Lets take an 8 bit binary code word as an example, for simplicity's sake. The principle is the same on larger numbers.

The bits represent powers of 2 like this:

1 1 1 1 1 1 1 1

128 64 32 16 8 4 2 1

if any bit is set to one it has that value.

so for example

1 0 1 0 1 0 1 0

(remember 128 64 32 16 8 4 2 1 )

would be 128+32+8+2 = 170

So from 0 to 128+64+32...+1( total of 255 ) is the value range

To allow negative numbers to be represented the largest bit is considered to be negative.

now the same example from above would be

-128+32+8+2 = -86

the value range now is from

- 128 to 127

I hope this helps when you try to calculate bit shifts ( though I need to practice the signed or unsigned myself )

Terry

[This message has been edited by Terence Doyle (edited August 24, 2001).]

I've seen a number of posts with doubts on how to calculate 2's complement numbers.

The following is an explanation of how the calculation is made from a telecommunications course I'm doing:

Lets take an 8 bit binary code word as an example, for simplicity's sake. The principle is the same on larger numbers.

The bits represent powers of 2 like this:

1 1 1 1 1 1 1 1

128 64 32 16 8 4 2 1

if any bit is set to one it has that value.

so for example

1 0 1 0 1 0 1 0

(remember 128 64 32 16 8 4 2 1 )

would be 128+32+8+2 = 170

So from 0 to 128+64+32...+1( total of 255 ) is the value range

To allow negative numbers to be represented the largest bit is considered to be negative.

now the same example from above would be

-128+32+8+2 = -86

the value range now is from

- 128 to 127

I hope this helps when you try to calculate bit shifts ( though I need to practice the signed or unsigned myself )

Terry

[This message has been edited by Terence Doyle (edited August 24, 2001).]

Raising Flares debut album 'Ignition' out now

http://www.raisingflares.com

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)