1

I have 8 bit int zero = 0b00000000; and 8 bit int one = 0b00000001; according to binary arithmetic rule,

0 - 1 = 1 (borrow 1 from next significant bit).

So if I have:

int s = zero - one; 
s = -1; 
-1 = 0b1111111;

where all those 1s are coming from? There are nothing to borrow since all bits are 0 in zero variable.

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
Nodir Nasirov
  • 1,488
  • 3
  • 26
  • 44

2 Answers2

6

This is a great question and has to do with how computers represent integer values.

If you’re writing out a negative number in base ten, you just write out the regular number and then prefix it with a minus sign. But if you’re working inside a computer where everything needs to either be a zero or a one, you don’t have any minus signs. The question then comes up of how you then choose to represent negative values.

One popular way of doing this is to use signed two’s complement form. The way this works is that you write the number using ones and zeros, except that the meaning of those ones and zeros differs from “standard” binary in how they’re interpreted. Specifically, if you have a signed 8-bit number, the lower seven bits have their standard meaning as 20, 21, 22, etc. However, the meaning of the most significant bit is changed: instead of representing 27, it represents the value -27.

So let’s look at the number 0b11111111. This would be interpreted as

-27 + 26 + 25 + 24 + 23 + 22 + 21 + 20

= -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= -1

which is why this collection of bits represents -1.

There’s another way to interpret what’s going on here. Given that our integer only has eight bits to work with, we know that there’s no way to represent all possible integers. If you pick any 257 integer values, given that there are only 256 possible bit patterns, there’s no way to uniquely represent all these numbers.

To address this, we could alternatively say that we’re going to have our integer values represent not the true value of the integer, but the value of that integer modulo 256. All of the values we’ll store will be between 0 and 255, inclusive.

In that case, what is 0 - 1? It’s -1, but if we take that value mod 256 and force it to be nonnegative, then we get back that -1 = 255 (mod 256). And how would you write 255 in binary? It’s 0b11111111.

There’s a ton of other cool stuff to learn here if you’re interested, so I’d recommend reading up on signed and unsigned two’s-complement numbers.

As some exercises: what would -4 look like in this format? How about -9?

These aren't the only ways you can represent numbers in a computer, but they're probably the most popular. Some older computers used the balanced ternary number system (notably the Setun machine). There's also the one's complement format, which isn't super popular these days.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

Zero minus one must give some number such that if you add one to it, you get zero. The only number you can add one to and get zero is the one represented in binary as all 1's. So that's what you get.

So long as you use any valid form of arithmetic, you get the same results. If there are eight cars and someone takes away three cars, the value you get for how many case are left should be five, regardless of whether you do the math with binary, decimal, or any other kind of representation.

So any valid system of representation that supports the operations you are using with their normal meanings must produce the same result. When you take the representation for zero and perform the subtraction operation using the representation for one, you must get the representation such that when you add one to it, you get the representation for zero. Otherwise, the result is just wrong based on the definitions of addition, subtraction, zero, one, and so on.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278