Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Sample Examples
2.
Solution Approach 
2.1.
Steps of algorithm
3.
Implementation in C++
4.
FAQs
5.
Key takeaways
Last Updated: Mar 27, 2024

2's complement

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The Binary Number System is one of the most widely utilized numerical representation schemes in digital systems. There are just two symbols or potential digit values in the Binary System: 0 (off) and 1 (on). Any device with only two functioning states or conceivable circumstances is represented by this term.

There are two sorts of Binary numbers complement 1's complement and 2's complement. Invert the supplied integer to get the 1's complement of a binary number. The 1's complement of the binary number 110010, for example, is 001101. To acquire the 2's complement of a binary number, add 1 to the least significant bit of the provided value (LSB). For example, the binary number (110010) 2's complement is (001101) + 1 = 001110

Also Read, Binary to Hex Converter and C Static Function.

Sample Examples

Example-1 −  Find 2's complement of binary number 10101100.
Invert each bit of a given binary number, which will be 01010011. Then add 1 to the LSB of this result, i.e., 01010011+1=01010100, which is the answer.

 
Example-2 −  Find 2's complement of binary number 10011001.
Invert each bit of the given binary number, 01100110. Then add 1 to the LSB of this result, i.e., 01100110+1=01100111, which is the answer.

You can also read about dynamic array in c, Tribonacci Series and Short int in C Programming

Solution Approach 

We will first find the one's complement of the binary number by just inverting the bits, i.e., 0's to 1's and vice versa. Then we will add 1 to the complementary of the binary number and finally print the output. 

Steps of algorithm

  1. Store the binary number as a string 
  2. Traverse the string, and if found 0, make it 1, and vice versa. 
  3. Finally, add 1 to the string. 
  4. Print the final output in the form of a string. 
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
void findOnesComplement(string & input)
{
	for (int i = 0; i < input.size(); i++)
	{
		if (input[i] == '0')
		{
			input[i] = '1';
		}
		else
		{
			input[i] = '0';
		}
	}
	return;
}
void findTwoComplement(string & input)
{
	// intitliaze carry = 1, because we will add 1
	// to the string in the twos complement
	int carry = 1;
	int n = input.size();
	for (int i = n - 1; i >= 0; i--)
	{
		int value;
		// storing the current value of the character in value variable  
		if (input[i] == '1')
			value = 1;
		else
			value = 0;
		int sum = value + carry;
		// if sum variable exceed 1, then carry will move forward
		if (sum > 1)
		{
			carry = 1;
			input[i] = '0';
		}
		else
		{
			input[i] = (char)(sum + '0');
			carry = 0;
			break;
		}
	}
	// if there is still carry, first print the carry
	// and then we will print the updated string
	cout << "2's Complement: ";
	if (carry > 0)
	{
		cout << carry;
	}
	// print the 2's complement string
	cout << input << endl;
}

int main()
{
	// storing the input binary number as the form of string
	string input = "1000111";
	cout << "Input Binary Number: " << input << endl;
	// finding ones complement intiallly
	findOnesComplement(input);

	cout << "Ones Complement: " << input << endl;
	// finding 2's complement
	findTwoComplement(input);
}

 

Output: 

Input Binary Number: 1000111
Ones Complement: 0111000
2's Complement: 0111001

For better understanding, you can implement on Online C Compiler.

FAQs

  1. What are binary numbers? 
    One of the four types of number systems is a binary number system. Binary numbers are represented by simply two symbols or digits in computer applications, namely 0 (zero) and 1 (one). The base-2 numeral system is used to express the binary numbers. A binary number, for example, is (101)2. In this approach, each digit is referred to as a bit.
     
  2. What are the uses of 2's complement?   
    The 2's complement of Binary numbers is used in various ways, primarily in signed Binary number encoding and different mathematical operations on Binary numbers, such as additions and subtractions. Because the representation of 2's complement is unambiguous, it is highly helpful in computer number representation. 
     
  3. Write 222 in binary?  
    222 in binary will be written as 11011110. 

Key takeaways

In this article, we discussed 2's complement and saw some sample examples to understand it better. Then we discussed the solution approach along with the algorithm and code in c+. We hope you understand the problem and solution properly. Now you can do more similar questions. 

Also read reverse a number.

If you are a beginner, interested in Coding, and want to learn DSA, you can look for our guided path for DSA, which is free! 

Thank you for reading. 

Until then, Keep Learning and Keep Coding.

Previous article
Delete an element from the Array
Next article
2's Complement Subtraction
Live masterclass