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.
This operation reverses each bit in the binary number. For instance, considering a binary number = 1001001
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.
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:
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.
#c++ #cplusplus #programming-c