# Binary representation in Integers

Page 1 of 1

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

### #1 toad87

• D.I.C Head

Reputation: 9
• Posts: 197
• 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

• Programming Theoretician

Reputation: 5187
• Posts: 13,917
• 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

• D.I.C Head

Reputation: 9
• Posts: 197
• 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

• D.I.C Addict

Reputation: 118
• Posts: 651
• 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

• D.I.C Head

Reputation: 9
• Posts: 197
• Joined: 21-May 12

## Re: Binary representation in Integers

Posted 27 October 2013 - 11:17 AM

dbasnett, 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

• "Hurry up and wait"

Reputation: 917
• Posts: 2,358
• 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

• D.I.C Addict

Reputation: 118
• Posts: 651
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }