Numbers are an essential part of our everyday lives, and mathematical calculations are entirely based on them. Numbers are primarily used in computers for various arithmetic operations, logical operations, and so on.
There aren’t just numbers on one side of the scale when it comes to numbers.
Numbers are also present on the Number scale to the left of the Zero(0), which we consider to be negative numbers.
Computers accept input in the form of numbers because, as we all know, machines only understand 0’s and 1’s, making it easier for machines to understand.
However, there are times when we need to perform operations on negative numbers or store negative numbers.
Now the question is, how are negative numbers stored? With a -1,-0. Do computers really understand what -1 and -0 mean? or is it an accurate representation? If this is not the correct way to represent a negative number, then what is it?
We can answer all of these questions by understanding the representation of numbers in the computer system below:
In every computer system, we have two types of numbers
- Unsigned
- Signed
UNSIGNED NUMBERS
Numbers that are always positive.
- The processor can easily store positive numbers.
Let us understand how unsigned numbers are represented:
Ex: 2, 5, 8.
value | Binary Representation |
2 | 0010 |
5 | 0101 |
8 | 1000 |
SIGNED NUMBERS
Numbers either positive or negative, half positive.half negative.
- In ALU operations are performed on signed numbers.
Let us understand how a signed number is represented:
There are 3 proposed methods for the representation of a signed number, we will look into each method and understand which method is most effective and acceptable worldwide by the machines.
Method 1:Sign magnitude
Method 2:1’s Complement
Method 3:2’s Complement.
Ex: 3-bit signed number:
Sign Magnitude
Value | Sign Magnitude |
3 | 011 |
2 | 010 |
1 | 001 |
0 | 000 |
-1 | 101 |
-2 | 110 |
-3 | 111 |
- In every system, positive numbers will remain as it is.
- 0 on MSB(Most-significant bit: Left most bit) indicated the number is positive.
- 1 on MSB(Most-significant bit: Left most bit) indicated the number is negative.
Eg: In 011 , Here 0 is MSB, which tells about the sign. Which is positive. And 11 Tells the magnitude.
Thus,011 represents the positive number 3.
Eg2: In 101 , here 1 is MSB, which tells about the sign. Which is negative. And 01 Tells the magnitude.
Thus,101 represents the negative number -1.
- For a 3-bit we should have 8 values.
- When we consider that way the 8th value is 100
100 = 1 | 0 0 = -0 which is not possible .
↓ ↓
Sign magnitude
Since 1 represents a negative number and 00 represents a value 0
Cause -0 does not exist in the number system.
Hence it is not possible, we don’t use this representation method.
1s Complement
Value | 1’s Complement |
3 | 011 |
2 | 010 |
1 | 001 |
0 | 000 |
-1 | 110 |
-2 | 101 |
-3 | 100 |
- Positive numbers are represented as it is. Only negative numbers are converted to 1’s complement.
- IN 1’s Complement, if there is a 3-bit number,
- The number is positive if MSB is 0.
- The number is negative if MSB is 1.
Ex: 010
0 | 10 = 2
The complement of 1 is: 1 | 00 = – 2
- For 3-bit numbers, we have 8 values, thus for the 8th value if we consider -4 which is 111 after 1’s complement it is represented as 000 which again is -0. This is a non-existent number hence this method is also not possible to represent signed numbers.
2’s Complement
Value | 2s Complement |
3 | 011 |
2 | 010 |
1 | 001 |
0 | 000 |
-1 | 111 |
-2 | 110 |
-3 | 101 |
-4 | 100 |
- Positive numbers are represented as it is. Only negative numbers are converted to 2’s complement.
- The number is positive if MSB is 0.
- The number is negative if MSB is 1.
Ex:
1: 0|01
2s complement of 1:1|11
- For 3-bit we need 8 values, considering the 8th value as 100
Then 100 = -4,
And 2’s complement of 4 i.e 100 is also 100.
Hence there is only one 100 representing -4
- Hence 2’s complement method is very effective in representing the signed numbers. We can say that all negative numbers are represented using 2’s complement in the system.
- Why 2’s complement is used because it does not produce -0 and gives a unique 0 value unlike other 2 methods
Shortcut to find 2s complement:
From the right-hand copy as it is to get the first 1.Once you get the first 1 invert everything.
Ex: 0 0 1
↓
1(first 1 found)
↓
2’s complement :1 1 1(remaining digits inverted)
Ex: 0 1 0
↓
1 0 (first 1 found)
↓
2’s complement :1 1 0(remaining digits inverted).
- We can expand this explanation by using 4-bit, 5-bit, and so on as well. Let us understand with a few examples:
Ex: number 5
+5 = 0 1 0 1
-5 = 1 0 1 1
Ex: number 26
+26 = 0 1 1 0 1 0
-26 = 1 0 0 1 1 0