- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
1’s and 2’s complement of a Binary Number?
Binary Number is expressed in base 2. It uses only two digits ‘0’ and ‘1’. Each digit in a binary number is a bit.
Sample binary Number − 0100010111
1’s Complement
One's complement of a binary number is obtained by reversing the digits of the binary number i.e. transforming 1 with 0 and 0 with 1.
Example
1’s Complement of 101100 = 010011
2’s Complement
Two’s complement of a binary number is obtained by adding one to the one’s complement of a binary number i.e. 1’s complement + 1.
Example
2’s complement of 101101 is 010011.
Example Code
Code to find One’s and two’s complement −
#include <iostream> #include<string.h> using namespace std; int main() { char binary[10] = "01001011"; cout<<“Binary number is ”<<binary; //once complement.... int length = strlen(binary); for(int i=0;i<length;i++) { if(binary[i] == '0') { binary[i]= '1'; } else binary[i] = '0'; } cout<<“One’s Complement is ”<<binary<<endl; // cout<<binary[length-1]; for(int i = length-1; i>=0; i--) { // cout<<binary[i]; if(binary[i] == '0') { binary[i] = '1'; //cout<<binary[i]; break; } else { binary[i] = '0'; } } cout<<“Two’s complement is ”<<binary; return 0; }
Output
Binary number is 01001011 One’s complement is 10110100 Two’s complement is 10110101
- Related Articles
- 1's Complement vs 2's Complement
- Draw a Turing machine to find 1’s complement of a binary number
- Draw a Turing machine to find 2’s complement of a binary number
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- Design a Moore machine to generate 1's complement of a binary number.
- 10’s Complement of a decimal number?
- 1's complement notation
- Previous number same as 1’s complement in C++
- 2's complement notation
- 2's complement fractions
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++

Advertisements