2's complement in C++
Try it first
Find Two's Complement
Give it a try before seeing the solution in this article.
Track completion, mastery, and revision.
Understanding 1's and 2's Complement
In computer science, representing negative numbers in binary is crucial. The most common method to achieve this is 2's complement.
- 1's Complement: Obtained by inverting all the bits of a binary number (replacing
1s with0s and0s with1s). - 2's Complement: Obtained by adding
1to the least significant bit (LSB) of the 1's complement.
Why use 2's complement?
It simplifies hardware design by allowing the CPU to perform subtraction using the same addition circuitry. It also avoids the problem of having "positive zero" and "negative zero" (which occurs in 1's complement representation).
Step-by-Step Example
Let's find the 2's complement of the binary number 101010:
- Original Binary Number:
101010 - Find the 1's Complement (Invert the bits):
010101 - Find the 2's Complement (Add 1 to the LSB):
010101 (1's complement) + 1 (add 1) -------- 010110 (2's complement)
Algorithm to Find 2's Complement
To implement this logic in C++ using character arrays (strings), we can follow these steps:
- Input Validation: Read a binary string of a fixed size.
- Generate 1's Complement:
- Loop through the binary string from left to right.
- If the character is
'1', change it to'0'. - If the character is
'0', change it to'1'. - If any other character is encountered, flag an error and terminate.
- Generate 2's Complement:
- Start from the rightmost bit (least significant bit) and move leftward.
- Initialize a
carryvariable to1(since we are adding 1 to the 1's complement). - If the current bit is
'1'andcarryis1, the result bit becomes'0'andcarryremains1. - If the current bit is
'0'andcarryis1, the result bit becomes'1'andcarrybecomes0. - If
carryis0, the remaining bits stay the same as the 1's complement.
C++ Implementation
Below is the complete C++ program to find the 1's and 2's complement of a 6-bit binary number.
// Program to find two's complement of a binary number
#include <iostream>
#define size 6
using namespace std;
int main() {
char binary[size + 1], one[size + 1], two[size + 1];
int i, carry = 1, fail = 0;
cout << "Input a " << size << " bit binary number: \";
cin >> binary;
for (i = 0; i < size; i++) {
if (binary[i] == '1') {
one[i] = '0';
} else if (binary[i] == '0') {
one[i] = '1';
} else {
cout << "Error! Input the number of assigned bits." << endl;
fail = 1;
break;
}
}
one[size] = '\0';
for (i = size - 1; i >= 0; i--) {
if (one[i] == '1' && carry == 1) {
two[i] = '0';
} else if (one[i] == '0' && carry == 1) {
two[i] = '1';
carry = 0;
} else {
two[i] = one[i];
}
}
two[size] = '\0';
if (fail == 0) {
cout << "The original binary = " << binary << endl;
cout << "After ones complement the value = " << one << endl;
cout << "After twos complement the value = " << two << endl;
}
}
Sample Output
Input a 6 bit binary number: 101101
The original binary = 101101
After ones complement the value = 010010
After twos complement the value = 010011
Finished reading?