> [name=AmazingCeazer]
> [time=Mon, Jun 16, 2025 11:18 PM]
> [color=#aaed63]
# INTRODUCTION
Negative numbers are stored in what we call variables,variables can differ E.g integers,float,doubles,strings,booleans etc. So with this there are multiple ways for computers to store negative numbers? Have you ever heard about signed-magnitude? Do you know what are the benefits of two’s complement that make it the way for representing signed numbers?
Well, let me try to answer all these questions
What is this? And what it isn’t?
This is a gentle introduction. The goal is to give you some intuition into how different representations work and learn how to work with them. I expect that you are at least a bit familiar with programming.
We’ll be talking only about integers. Floats are very special beasts and are out of the scope of this article.
So, how do you store a number?
If you are familiar with words like bit, byte, and binary representation, feel free to skip the first two paragraphs.
Now lets use an Example:
Let’s say you are a computer (brr brr) and a programmer asks you to store some nice number for him, let’s say 5. How do you remember it? Obviously, there is no paper for you to write it down, so you need to store it in your memory. However, your world is composed of only so called bits: ones and zeros. There is no place in your memory to which you can write 5, instead, you need to store it in its binary representation.
The binary representation of 5 is 101. So you might be tempted to write 101 to some address in your memory. But can you? It turns out that isn’t possible either. Your memory is fragmented into bytes 8-bit segments and you cannot possibly work with anything smaller than one byte. So you are bound to pad your 101 with zeros so its length is 8 and then you can store it!
So, now you have 0000 0101 somewhere in your memory (the middle space is there just for readability). Your programmer is happy. You can remember stuff! So he tries to make it harder and asks you to store -5.
oh!wahala There is no minus sign(-) in your binary world at all. You only know 0(zeros) and 1(one). Nonetheless, you think a little. And come up with an idea! What about reserving another byte? This byte can indicate whether the number in the byte immediately next to it is negative or positive!
Therefore, you store something like: minusbyte, 0000 0101 in your memory. Now you need two bytes for your -5.
But wait a second. Do you really need a whole byte for something that can take just two values? Not really, we can include sign in the byte with 5. We can interpret 1 on the position of the most significant bit as minus, and 0 as a plus.
So we end up with 1000 0101 for our -5.
Remove 1 and it becomes 0000 0101 5.
Whoa! We just discovered a new representation of negative numbers: signed magnitude!
Let’s analyze this new beast. What is the greatest number we can represent? From the 8 bits in byte, we use one to store the sign and the rest to store the magnitude. From this, we obtain that the greatest magnitude is 2⁷ — 1 = 127. If you don’t see why, I encourage you to take a piece of paper and write down all the powers corresponding to different bits (1, 2, 4, 8, …) and sum the first seven.
Because the greatest magnitude is 127, we can represent any integer between -127 and 127.
Now observe that this range is symmetric: we have the same number of positive and negative numbers. This might not look unusual but it is! Two’s complement which we will introduce shortly doesn’t have this property.
How do we represent zero? If you’ve never seen signed magnitude representation before, please, pause and think about this. An interesting phenomenon occurs. There are two ways to represent 0. One is 0000 0000, the other 1000 0000.
## How big a problem is this?
Surprisingly, quite often in a computer we need to check something against zero. But now, when we have two zeros, against what zero we should check? Soon we will start to think if one zero is superior to the other? And although the problem of two zeros is solvable it can be quite a headache..
One last problem with this representation is the way we do subtraction. If you think about this, you quickly find that there are multiple questions we would have to answer and cases to think about. It might happen that we want to subtract two positive numbers, negative and positive, positive and negative and so on. Most importantly, we need some special circuit to do the subtraction for us.
However, what if we can’t have a circuit for subtracting? Either we don’t have enough money or physical space. Is it possible to come up with something that needs only an adder?
Integer Values
For integers, there are three typical negative value representations:
Sign-Magnitude, in which the most significant (leftmost) bit is dedicated to representing the sign of the value (1 for negative, 0 for positive). Given the binary value 00101010 (42 in decimal), the representation of -42 would be 10101010.
One’s-Complement, in which all the bits of the value are inverted to represent the negative value. Given the binary value 00101010 (42 in decimal), the representation of -42 would be 11010101.
Two’s Complement, in which all the bits of the value are inverted and then a 1 is added. Given the binary value 00101010 (42 in decimal), the representation of -42 would be 11010110.
By far, the most popular negative integer representation these days is two’s-complement, but we do occasionally run into the others in some environments. (The C language standards, since C99, require the use of one of these three representations to represent negative integers.)
Notice that in all three representations, the most significant (leftmost) bit is 1 if the value is negative, and 0 if the value is positive.
These are not the only three possible representations for integers.
Two’s complement
Let’s return to the size of a byte again. We already observed that the amount of information we can store in one byte is limited. We cannot store random huge numbers. Let’s see what happens when we try to add 100 and 200.
100)_10 = (0110 0100)_2
(200)_10 = (1100 1000)_2
0110 0100 + 1100 1000 = 1 0010 1100
Ouch. We cannot store 9 bits. What usually happens is that only the 8 least significant bits get stored so we end up with 0010 1100 and if we convert this to decimal we get 100 + 200 = 44. We call this an overflow. That looks quite annoying. But what if there is a way to use this property to simulate subtraction with addition? Indeed, we were adding two big numbers and ended up with something small.
Note: you can think about the previous addition in terms of modular arithmetics. In our case, we are calculating everything mod 256. That means we calculate the remainder of the result of the addition when divided by 256. A simple way to interpret this is to think about a standard 12-hour clock. When you are working on a 4-hour homework and start at 10 PM, you finish it at 2 AM not 14 PM. The same thing happens here but we have 256 hours instead of 12.
Now, the most difficult part follows. Let's Brace yourself!
For any number k it surely holds that 0 = k + (-k) = 0000 0000. However, in our world, we are also completely happy with the result k + (-k) = 1 0000 0000.
This simple fact leads to the most elegant representation of negative numbers one can think of. For each positive number P we will create a negative number N with the property that P + N = 1 0000 0000.
If you are not sure how this works, try to think in decimals. Let’s say that you have a memory of only one digit, so you can remember anything from 0 to 9. Now, how do you find -3? You would like P + N = 10. For P = 3, only satisfying N is 7.
Now, let’s get back to the world of ones and zeros. We need to find a way for computing negative numbers corresponding to some positive.
We know that P + N = 1 0000 0000, therefore we could rearrange this to get N = 1 0000 0000-P.
However, this leads to subtracting. And I promised that we can do it without subtracting.
2's complement
Using 2's complement method
Primarily, we use 2's complement to make addition and subtraction simple at the hardware level.
N bit number why don't we let the MSB (The leftmost bit) represent the sign. A 0 will represent that it is positive and a 1 will represent a negative number. This would be simple, right?
Let's take an example.
A Step by Step conversion of -11 into 8-bit
Consider the number 11. We can represent this as
1011 as per our design.
Now consider -11. This would be 1111 1111.
Now let's say we want to add these numbers. 11+ (-) = 0. But this is not what we get if we use traditional binary addition to add 1011 1111 and 1111 1111. And remember that we want it to be simple (the old binary addition rules should apply).
Therefore a new method was formulated. In this method, the MSB still represents the sign. A 0 represents a positive number and a 1 represents a negative number.
Now a positive number is the same in 2's complement (as our previous method). So 11 will be 1011 1111.
But -11 is different. Here's how we get -11.
First flip all the bits in the positive number.
Add 1 to the resultant flipped number.
So, 1011 1111 (11) becomes 1000 000.
Now we add 1 to it. 1000 0000 + 0000 0001 = 1000 0001.
And this is our 2's complement representation of -11. (1000 0001).
Now we can perform the simple binary addition algorithm and have it work perfectly.
11 + (-11) = 0
1011 1111 + 1000 0001 = 0000 0000 just like we would expect.
Corresponding Hexadecimal value
The hexadecimal system, also known as base-16, is a number system that uses 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
so our 11 will be 11B on the Hexadecimal
This system is commonly used in computing because it allows for more compact representation of numbers. In the hexadecimal system, each digit represents a power of 16. For example, the number FF in hexadecimal can be represented as 15 * 16^1 + 15 * 16^0, or 255 in decimal.
Converting from decimal to hexadecimal
To convert a decimal number to hexadecimal, you can use the following steps:
Divide the decimal number by 16.
Take the remainder of the result from step 1 and convert it to the corresponding hexadecimal digit. If the remainder is 10 or greater, you can use the letters A, B, C, D, E, or F to represent the values 10 through 15, respectively.
Divide the result from step 1 by 16 again, and repeat steps 2 and 3 until the result is 0.
Write down the hexadecimal digits in the reverse order that you obtained them. The first digit you obtained will be the least significant digit, and the last digit you obtained will be the most significant digit.
For example, to convert the decimal number 11 to hexadecimal, you can do the following:
Divide 11 by 16: 225 / 16 = 15, remainder 15
Convert the remainder to a hexadecimal digit: 15 = F
Divide the result from step 1 by 16 again: 15 / 16 = 0, remainder 15
Convert the remainder to a hexadecimal digit: 15 = F
Write down the hexadecimal digits in reverse order: F, F
Therefore, the hexadecimal representation of 255 is FF.
Conclusion
You probably noticed that computers can store greater numbers than those that fit into one byte. Nowadays, you will most often encounter 4-byte integers which can store numbers up to a few billions. However, you can straightforwardly extend what was described here to any number of bytes.
If you are coming from Python, you might have noticed that there is nothing like an upper limit on the size of your integers. This is because Python has a rather intriguing (and inefficient) way of storing numbers that allows it to allocate as much memory as the number requires.
Hope you learned something new. Have a nice day!
{"title":"HOW COMPUTERS SAVE NEGATIVE NUMBERS(Using -11 as acase study)","contributors":"[{\"id\":\"7cf698cc-2693-48f9-b4b6-485a5338a5f1\",\"add\":11845,\"del\":94}]","description":"[] INTRODUCTIONNegative numbers are stored in what we call variables,variables can differ E.g integers,float,doubles,strings,booleans etc. So with this there are multiple ways for computers to store negative numbers? Have you ever heard about signed-magnitude? Do you know what are the benefits of two’s complement that make it the way for representing signed numbers?"}