- 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
C Program to find two’s complement for a given number
The two’s complement for a given binary number can be calculated in two methods, which are as follows −
Method 1 − Convert the given binary number into one’s complement and then, add 1.
Method 2 − The trailing zero’s after the first bit set from the Least Significant Bit (LSB) including one that remains unchanged and remaining all should be complementing.
The logic to find two’s complement for a given binary number is as follows −
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'; printf("Two's complement of binary number %s is %s
",num, two);
The logic for finding one’s complement from a given binary number is −
for(i = 0; i < SIZE; i++){ if(num[i] == '0'){ one[i] = '1'; } else if(num[i] == '1'){ one[i] = '0'; } } one[SIZE] = '\0'; printf("Ones' complement of binary number %s is %s
",num, one);
Example
Following is the C program to find two’s complement for a given number −
#include<stdio.h> #include<stdlib.h> #define SIZE 8 int main(){ int i, carry = 1; char num[SIZE + 1], one[SIZE + 1], two[SIZE + 1]; printf("Enter the binary number
"); gets(num); for(i = 0; i < SIZE; i++){ if(num[i] == '0'){ one[i] = '1'; } else if(num[i] == '1'){ one[i] = '0'; } } one[SIZE] = '\0'; printf("Ones' complement of binary number %s is %s
",num, one); 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'; printf("Two's complement of binary number %s is %s
",num, two); return 0; }
Output
When the above program is executed, it produces the following result −
Enter the binary number 1000010 Ones' complement of binary number 1000010 is 0111101 Two's complement of binary number 1000010 is 0111110
- Related Articles
- Two’s Complement
- Java Program to retrieve the current bits in a byte array in two’s-complement form
- C program to find Fibonacci series for a given number
- C program to rotate the bits for a given number
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- C++ Program to find the smallest digit in a given number
- Write a Golang program to find the sum of digits for a given number
- C++ Program to Find the Number of Permutations of a Given String
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- C Program to convert a given number to words
- C program to find if the given number is perfect number or not
- Java program to find a cube of a given number
- C Program for efficiently print all prime factors of a given number?
- C Program to find the given number is strong or not
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach

Advertisements