TutorialStudyMite

Program to find 1's complement of a binary number in C++

Beginner friendly

Try it first

1's Complement of a Binary String

Give it a try before seeing the solution in this article.

Give it a try

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:

  1. Input Acquisition: Read the binary number as a string or character array.
  2. Initialization: Create a new string or character array of the same length to store the resulting 1's complement.
  3. 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.
  4. 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 size and char arrays: This program uses #define size 6 and C-style char arrays. This means it's designed to only process binary numbers exactly 6 bits long. For more flexible input lengths, one would typically use std::string in 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 before size characters were read, causing binary[i] to be a null character (\0) when i reaches 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 10101 is only 5 bits long, but the program expects 6 bits. When cin >> binary; reads 10101, it stores these characters in binary[0] through binary[4] and places a null terminator (\0) at binary[5]. The for loop iterates from i = 0 to i < size (i.e., i < 6). When i becomes 5, binary[5] is '\0'. Since '\0' is neither '0' nor '1', the else block is executed, printing the error message and setting fail = 1, which prevents the complement from being displayed. This behavior highlights the fixed-size limitation of the char array.

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.