Computers stores everything in Base 2 or binary format than Base 10 like what humans usually count with. This article is to brief how computer stores negatives numbers generally and the most prevalent approach of two’s complement. Understanding this will explain why a SByte in C# is -128 to +127.
Overall we need a method to represent both positive and negative numbers as binary which involves minimum computation to perform arithmetic operations and follows mathematical principles like not have negative zero.
Base 2 Compared To Base 10
As an overview in English we generally represent Base 10 using 0, 1, 2 to 9. Similarly Base 2 is expressed with 0’s and 1’s. Below are few number conversions as a refresher.
Value | Base 10 | Base 2 |
---|---|---|
7 | 7 | 111 |
10 | 11 | 1010 |
14 | 15 | 1110 |
Negative Numbers
When it came to representing negative numbers things gets bit complicated. Different approaches are there like Sign and Magnitude, One’s Complement, Two’s Complement. Due to issues in other approaches two’s complement is most widely used.
Sign and Magnitude
In this method, one bit will be a reserved sign bit. If the sign bit is 0, the number is positive, and if it’s 1 the number is negative. Rest of bits will represent magnitude. Below is number 10 on 5 bit system,
Value | Sign Bit (bit 5) | 2 ^3 (bit 4) | 2 ^2 (bit 3) | 2 ^1 (bit 2) | 2 ^0 (bit 1) |
---|---|---|---|---|---|
+10 | 0 | 1 | 0 | 1 | 0 |
-10 | 1 | 1 | 0 | 1 | 0 |
There are many drawbacks to above approach. Few main ones are
- In above representation when the Sign bit is 0 or 1 and rest of magnitude bits is 0 will result in two ways number Zero is getting represented. There is +ve and -ve Zero.
- Simple Operations like addition and subtractions itself is quite complicated due to sign bit. Just adding +10 with -10 in binary as in above table you will get an incorrect result of 10100.
One’s Complement
In One’s Complement approach, all the bits are flipped to represent a negative number. It also has a sign bit and below is a sample representation,
Value | Sign Bit (bit 5) | 2 ^3 (bit 4) | 2 ^2 (bit 3) | 2 ^1 (bit 2) | 2 ^0 (bit 1) |
---|---|---|---|---|---|
+10 | 0 | 1 | 0 | 1 | 0 |
-10 | 1 | 0 | 1 | 0 | 1 |
Drawbacks of this approach
- Similar to other approach there are two representations for zero. Positive zero is represented as all 0s (00000), and negative zero is represented as all 1s (11111). Just adding +10 with -10 in binary as in above table you can notice the complexity since the result is -ve zero. This can lead to confusion in arithmetic and comparison operations.
- Regular mathematical operations can be done on binary but there are scenarios where carry over happens and is still complicated. Addition of +3 (binary: 00011) and -2 (binary: 11101, the one’s complement of +2) will result in a carry over error.
Two’s Complement
This is the most widely approach because it simplifies all arithmetic operations. In this representation, negative numbers are obtained by taking the two’s complement of the positive number. To do this, invert all the bits (change 0s to 1s and vice versa) and then add 1 to the result.
Steps | Value | Sign Bit (bit 5) | 2 ^3 (bit 4) | 2 ^2 (bit 3) | 2 ^1 (bit 2) | 2 ^0 (bit 1) |
---|---|---|---|---|---|---|
+10 | 0 | 1 | 0 | 1 | 0 | |
Step 1 is do do ones complement | 1 | 0 | 1 | 0 | 1 | |
Add 1 to above to get -10 | -10 | 1 | 0 | 1 | 1 | 0 |
Advantages and drawbacks,
- This has none of drawbacks of other two approaches. Zero is only Zero as 00000 and all arithmetical operation are straightforward binary calculations
- One minor drawback is in regards to overflow scenarios. If the result of an addition or subtraction exceeds the range, overflow occurs, and the result will not be accurate. This can be handled by ensuring the right selection of data types. Various languages and IDE’s has overflow flags to notify this.
SByte in C# is -128 to +127
To calculate the range of a type like 8-bit Sbyte, we can use the formula as below. C# uses two’s complement method to store the negative values. Even though Sbyte is 8 bit one bit is used as sign bit so only 7 bits are available for magnitude. The 7 bit then divided into -ve and +ve range.
Largest positive number for a 8 bit system is (2^(n-1)) – 1 that is (2^7) – 1 is +127
Lowest negative number for a 8 bit system is 2^(n-1) that is 2^7 is -128.
Leave a Reply