Two’s Complement: A Practical Mathematical Operation

Filed Under: C++
Twos Complement Featured Image

Two’s Complement is a mathematical operation on Binary Numbers. This operation has many real-life applications like the implementation of subtractions and the storage of negative integers.

In this article, we will do an in-depth analysis of Two’s Complement, which includes its calculation, applications, and implementation in C++.

Before we understand the concept of Two’s Complement, we need to study One’s Complement.

Understanding One’s Complement

This operation reverses each bit in the binary number. For instance, considering a binary number = 1001001

Twos Complement Ones
One’s complement of a binary number

As seen in the above figure, One’s Complement is the reversal of each bit of a binary number.


Understanding Two’s Complement

In addition to applying One’s Complement, Two’s Complement include adding 1 to the One’s Complement. Carrying forward the previous example, we see:

Two Complement Example
Example of Two’s Complement

As a result, Two’s complement involves “reversing the bits” and “adding 1 to the reversed bits”. This operation may seem trivial at first, but it forms the underlying concept of all subtractions in modern systems.


Applications of Two’s Complement

In mathematical terms, Two’s complement of N-bit binary number can be defined as a complement with respect to 2^N. In other words, the addition of a binary number and its Two’s complement gives us 2^N.

Storage of Signed Integers

The computer understands the language of 0’s and 1’s. It has no clue regarding signs and notations. Therefore, programmers designed a method of storing negative numbers using Two’s Complement.

Consider 8-bit signed numbers. There are exactly 2^8 (256) values than can be stored by an 8-bit number. In order to accommodate positive and negative numbers, we divide the space in two halves.

The first half – (0 - 127) denotes the positive binary numbers, whereas the rest (128 - 255) denote negative binary numbers.

Twos Complement 8bit Numbers
8-bit signed number system

As a result, we call the most significant bit in signed integers as the sign bit, whereas the rest of the number defines its value.

The conversion of a number to its negative value is possible by Two’s complement. For instance, consider the decimal number 19 (00010011) within an 8-bit storage space. The two’s complement of the above binary number gives us -19 (11101101).

Twos Complement Signed Number Edited
Signed Numbers as Two’s Complement

The operation works both ways. Consequently, the two’s complement of -19 (11101101) provides 19 (00010011).

In brief, two’s complement of a number provides its negation with respect to the decimal number system.


Subtraction using Two’s Complement

The previous knowledge of signed numbers forms as the basis of subtraction of numbers. For instance, we wish to calculate 30 - 19.

The above operation can be modified as 30 + (-19). We have converted a subtraction operation into an addition operation. Both the numbers 30 and -19 can be represented as binary numbers.

It may be clear to the reader, that for the calculation of -19, two’s complement will be used.

Twos Complement Subtraction Edited
Subtraction using Two’s Complement

The same principle of converting positive numbers to negative numbers according to the signs helps the processors to perform the operations.


Implementation of Two’s Complement in C++

#include<iostream>
using namespace std;

// Function to calculate Two's Complement
string cal_twos_complement(string num, int size){

	// String that stores the result
	string res = "";

	// Loop to calculate One's Complement
	for(int i=0; i<size; i++){

		// If 0 then add 1, otherwise add 0
		res += (num[i] == '0') ? '1' : '0';
	}

	// Loop to calculate Two's Complement
	for(int i=size-1; i>=0; i--){

		// Convert 1's into 0's until 0 is encountered from the right
		if(res[i] == '1')
			res[i] = '0';

		// Convert the first 0 from right to 1
		else{
			res[i] = '1';
			break;
		}
	}	

	return res;
}

int main(){

	// Binary number as string
	string num = "00010011";

	// Size of binary number
	int size = num.size();

	cout<<"Original Number:   "<<num<<endl;

	// Function to calculate two's complement
	string twos_complement = cal_twos_complement(num, size);

	// Print the two's complement
	cout<<"Two's Complement:  "<<twos_complement<<endl;

	return 1;
}

Output:

Original Number:   00010011
Two's Complement:  11101101

In the above code, we first calculate the One’s Complement and then use it to compute the Two’s Complement. The logic behind the evaluations is easy.


Conclusion

The concept behind the Two’s Complement may seem basic at first, but only after we study its applications, we can find out the hidden concepts.

We hope this article was easy to follow. Feel free to comment below for any questions on this topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages