6 Replies - 2198 Views - Last Post: 24 January 2013 - 09:48 AM

#1 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 318
  • Joined: 21-October 11

simple binary question (negatives)

Posted 21 January 2013 - 07:02 PM

there are various ways to represent negative numbers in a binary system, why doesnt a computer get confused?

For example, with 3 bits, ones compliment
010 = 2
101 can mean 7 or -2

as far as I can tell, other methods have problems as well

the question is, how do you avoid problems like this? why does a computer know that you're using a negative and not a straight up binary number?
Is This A Good Question/Topic? 0
  • +

Replies To: simple binary question (negatives)

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: simple binary question (negatives)

Posted 21 January 2013 - 07:05 PM

Certain data types are signed, and others are unsigned. Even at the assembly level, there are various jump statements that deal with signed values, and others that are similar but deal with unsigned values.
Was This Post Helpful? 1
  • +
  • -

#3 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 318
  • Joined: 21-October 11

Re: simple binary question (negatives)

Posted 21 January 2013 - 08:57 PM

View Postmacosxnerd101, on 21 January 2013 - 07:05 PM, said:

Certain data types are signed, and others are unsigned. Even at the assembly level, there are various jump statements that deal with signed values, and others that are similar but deal with unsigned values.

I dont think I understand you, a binary representation of a negative number doesnt use a sign, it uses its digits, and since it uses its digits, the string for a certain negative number can theoretically mean something else as well.

can you give more detail?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: simple binary question (negatives)

Posted 21 January 2013 - 09:29 PM

There are signed and unsigned variables. C/C++ both make uses of these types of variables. A signed variable can have positive or negative values, while an unsigned variable can only have positive values. So if you have a signed variable with the high bit as a 1, then it will be seen as a negative number. If you have an unsigned variable with the high bit as a 1, it's still positive. Operators will take this into account.

When you really get down into Assembly, you can see this happening just with the JUMP instructions. See the Intel x86 reference for an example.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: simple binary question (negatives)

Posted 22 January 2013 - 09:11 PM

So is this making sense now? Is this question resolved?
Was This Post Helpful? 0
  • +
  • -

#6 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: simple binary question (negatives)

Posted 24 January 2013 - 08:25 AM

No one has pointed out that 101 would be 5 or -3, not 7 or -2. 7 is 111, and -2 is 110 (two's compliment).

I know, I'm petty like that.
Was This Post Helpful? 1
  • +
  • -

#7 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: simple binary question (negatives)

Posted 24 January 2013 - 09:48 AM

Not petty enough. He specified that he is using one's complement so 101 is 5 or -2.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1