Unveiling the Binary Secrets: Subtraction at the Bit Level in Computers
Computer is known to perform various tasks which range from simple calculations to complex simulations. It is Imperative to know that these operations are performed at bit level on the computers. Computers represent these numbers based on its architecture. An example is a 64-bit architecture computer represent every number with 64 bit long binary number. The first bit represents the sign, while the next 11 bit represent characteristics and last 52 represent mantissa for fractional component.
Number System
Human operations are performed in decimal (base 10). While computer perform its operation in binary (base 2). Though this article will not cover the conversion from one base to another and addition in binary, it is necessary to have idea about these two topics. If you have no idea, you can read these two articles Number System Conversion and Binary Number Addition before proceeding.
Simple Calculation on Computers
While addition at the bit level might be easy for the computer, It is quite a cumbersome process for subtraction. Computers represent negative number in a bizarre manner which is known as two complement of a fixed bit length n.
One of the assumptions here is that we are dealing with a fixed bit length 8(8-bit architecture computer). The leading bit represents the sign (1 for negative and 0 for positive) and the other 7 bit is binary representation of the number we are dealing with. 8-bit means we can only deal with number from range -255(-2^8 - 1) through 0 and 255(2^8 - 1).
Two complement of a fixed bit length n
The formula is 2^n - a, where n = bit length (which assumed here to be 8) and a = the number we are finding its two-complement.
The steps for this are:
Convert the number to its binary digit.
flip the number (not gate).
Add 1, The result is the required two complement of bit 8 of the number.
It is imperative to know that for reversal, you follow the same method in chronological order.
Example 27 base 10
27 (base 10) = 00011011 (base 2)
flip (00011011) ->11100100
Add 1 -> 11100101
This is the required binary.
Subtraction at the Bit Level
If a number is greater than or equal to 0, computer represented the number in its binary form. If it is less than 0, it represents the number in the two complement of a fixed bit length n- here 8.
Example 1: 72 + (-54)
72 = 01001000
54 = 00110110
-54 = flip (00110110) -> (11001001) + 1 = 11001010
72 + (-54) = 100010010
We are discarding the rightmost bit that make our 8-bit 9-bit because this is 8-bit architecture 00010010
The leading bit is 0, so the number is positive, and we just simply need to convert back to base 10.
00010010 = 18 base 10 which is our answer
Example 2: 39 + (-89)
39 = 00100111
89 = 01011001
-89 = flip (01011001) -> (10100110) + 1 = 10100111
39 + (-89) = 11001110
This is exactly 8-bit and there is no need for discarding. But the leading bit is 1 which means this number is negative. so let us convert it to back to give us the character with negative part.
flip (11001110) -> (00110001) + 1 = 00110010
00110010= 50 base 10 which is our answer
-50 base 10 which is our answer
Conclusion
Subtraction at bit-level is quite complex and its understanding gives us the overview of how computer works at the bit-level. Understanding the concept behind it -which are number system and two complement of a fixed bit length n of a - gives more feasibility on other area of system architecture.
This article covered number system, binary representation, two complement of a fixed bit length n of a, and subtraction at bit-level of the computer.
Let's draw the veil here.
You can share your thoughts with me on Twitter here.
Happy Engineering!
Subscribe to my newsletter
Read articles from Mustapha Oyebamiji directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mustapha Oyebamiji
Mustapha Oyebamiji
Backend developer | Technical writer