6 Replies - 933 Views - Last Post: 28 October 2013 - 05:30 AM

#1 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Binary representation in Integers

Posted 26 October 2013 - 04:25 PM

One binary number can represent both a positive and negative integer right?

For example 1001 can be 9 or -7.

If this is true, how does the computer differentiate a positive and negative number?

I know if we wrote it out into into 8 bit,

9 = 0000 1001

and

- 7 = 1111 1001

But what if I had 1111 1111. It would be 255 or -1. But there aren't any more preceding bits to determine if it's a negative or positive.

Maybe I'm misunderstanding something. Can someone clear this up? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Binary representation in Integers

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Binary representation in Integers

Posted 26 October 2013 - 05:15 PM

The left most bit is used to tell the sign of the integer. As for how the computer tells the difference, check out One's Complement and Two's complement to learn more about how computers store presentation of signs for integer numbers.

:)

This post has been edited by Martyr2: 26 October 2013 - 05:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Binary representation in Integers

Posted 26 October 2013 - 05:27 PM

Ohhhhh I see now...In an 8 bit system you wouldn't have -255 to 255.

You would only be able to get -128 to 127...-128 being 10000000 and 127 being 01111111.


Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Binary representation in Integers

Posted 27 October 2013 - 07:56 AM

The determination of whether or not a number is signed or unsigned is based upon which flags are checked / not checked.
Was This Post Helpful? 0
  • +
  • -

#5 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Binary representation in Integers

Posted 27 October 2013 - 11:17 AM

View Postdbasnett, on 27 October 2013 - 07:56 AM, said:

The determination of whether or not a number is signed or unsigned is based upon which flags are checked / not checked.


Awww man...Now you confused me again...

Can you elaborate on these "flags"?
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Binary representation in Integers

Posted 27 October 2013 - 06:34 PM

Quote

The determination of whether or not a number is signed or unsigned is based upon which flags are checked / not checked.

This is not always true. In general, a number is signed if the MSB is set.

Not all instructions set the Sign Flag, so how would you know if the number is signed or not? You would check the Most Significant Bit.

mov     eax, -1

Will not set the Sign flag in the EFLAGS register.


mov     eax, 1
sub     eax, 2

This WILL set the sign flag in the EFLAGS register.

The CPU only sets the sign flag with certain instructions.

http://en.wikipedia..../FLAGS_register

The only time the Sign Flag is set:
  • All arithmetic operations except multiplication and division;
  • compare instructions (equivalent to subtract instructions without storing the result);
  • Logical instructions - XOR, AND, OR;
  • TEST instructions (equivalent to AND instructions without storing the result).

Was This Post Helpful? 0
  • +
  • -

#7 dbasnett  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Binary representation in Integers

Posted 28 October 2013 - 05:30 AM

The processor doesn't care for the most part is the most significant bit is set or not, but will turn on the sign flag(SF) if it is, or if it comes on. If you are working with an abstraction that says that a number is signed then monitoring the SF is important. If the abstraction is unsigned the SF doesn't matter.

SF turns on in both cases
signed byte = 0111 1111 + 1 = 1000 0000 is not legal
unsigned = 0111 1111 + 1 = 1000 0000 is legal

The other flag that is monitored is the carry flag(CF).

This post has been edited by dbasnett: 28 October 2013 - 05:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1