Program to find 1's complement of a binary number in C++
Try it first
1's Complement of a Binary String
Give it a try before seeing the solution in this article.
Track completion, mastery, and revision.
Understanding 1's Complement
In digital electronics and computer science, the 1's complement is a crucial operation for binary numbers, often used in arithmetic operations like subtraction.
The 1's complement of a binary number is another binary number obtained by inverting each bit: all 0s become 1s, and all 1s become 0s.
Example:
- Input:
101010 - 1's Complement:
010101
This article demonstrates how to find the 1's complement of a binary number using C++.
Algorithm for Finding 1's Complement
To compute the 1's complement of a binary number represented as a string, we can follow these steps:
- Input Acquisition: Read the binary number as a string or character array.
- Initialization: Create a new string or character array of the same length to store the resulting 1's complement.
- Iteration and Transformation: Loop through each character (bit) of the input binary string from left to right.
- If the current character is
'0', append'1'to the complement string. - If the current character is
'1', append'0'to the complement string. - Input Validation: If the current character is anything other than
'0'or'1', it indicates an invalid binary input. In this case, an error message should be displayed, and the process should terminate.
- If the current character is
- Output: If no errors were encountered, print the original binary number and its calculated 1's complement.
C++ Implementation
This C++ program implements the algorithm described above using a fixed-size character array.
Fixed-Size Array Approach
#include <iostream>
#define size 6 // Defines the fixed size of the binary number
using namespace std;
int main() {
int i, fail = 0;
char binary[size + 1], comp[size + 1]; // +1 for null terminator
cout << "Input a " << size << " bit binary number: ";
cin >> binary; // Reads input into the char array
for (i = 0; i < size; i++) {
if (binary[i] == '1') {
comp[i] = '0'; // If '1', complement is '0'
}
else if (binary[i] == '0') {
comp[i] = '1'; // If '0', complement is '1'
}
else {
// Handles invalid characters
cout << "Error! Input the number of assigned bits." << endl;
fail = 1; // Set fail flag
break; // Exit loop on error
}
}
comp[size] = '\0'; // Null-terminate the complement string
if (fail == 0) { // If no error occurred
cout << "The original binary number = " << binary << endl;
cout << "Ones complement the number = " << comp << endl;
}
return 0; // Indicate successful execution
}
Note on
sizeandchararrays: This program uses#define size 6and C-stylechararrays. This means it's designed to only process binary numbers exactly 6 bits long. For more flexible input lengths, one would typically usestd::stringin modern C++ and dynamically determine the length. The error message "Error! Input the number of assigned bits." in this context implies that the input contained characters other than '0' or '1', or that the input stream terminated beforesizecharacters were read, causingbinary[i]to be a null character (\0) whenireaches the end of the input.
Example Usage and Output
Here are examples demonstrating the program's execution with valid and invalid inputs.
Input a 6 bit binary number: 101101
The original binary number = 101101
Ones complement the number = 010010
Input a 6 bit binary number: 10101
Error! Input the number of assigned bits.
Note on the second output: The input
10101is only 5 bits long, but the program expects 6 bits. Whencin >> binary;reads10101, it stores these characters inbinary[0]throughbinary[4]and places a null terminator (\0) atbinary[5]. Theforloop iterates fromi = 0toi < size(i.e.,i < 6). Whenibecomes5,binary[5]is'\0'. Since'\0'is neither'0'nor'1', theelseblock is executed, printing the error message and settingfail = 1, which prevents the complement from being displayed. This behavior highlights the fixed-size limitation of thechararray.
Finished reading?