Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program to find two's complement for a given number
The two's complement is a mathematical operation used to represent negative numbers in binary form. It can be calculated using two methods −
- Method 1 − Convert the given binary number into one's complement and then add 1.
- Method 2 − Starting from the rightmost bit, copy all bits until the first '1' (including it), then complement all remaining bits.
Syntax
// One's complement: flip all bits // Two's complement: one's complement + 1
How Two's Complement Works
The two's complement process involves these steps −
- Find the one's complement by inverting all bits (0 becomes 1, 1 becomes 0)
- Add 1 to the one's complement result
- Handle any carry overflow during addition
Example: Finding Two's Complement
Following is the C program to find two's complement for a given binary number −
#include <stdio.h>
#include <string.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 (8 bits): ");
fgets(num, sizeof(num), stdin);
// Remove newline if present
num[strcspn(num, "
")] = '\0';
// Find one's complement
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("One's complement of %s is %s
", num, one);
// Find two's complement (add 1 to one's complement)
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 %s is %s
", num, two);
return 0;
}
Enter the binary number (8 bits): 1000010 One's complement of 1000010 is 0111101 Two's complement of 1000010 is 0111110
Method 2: Using Bitwise Operations
Here's an alternative approach using bitwise operators for integer values −
#include <stdio.h>
void printBinary(int num, int bits) {
for(int i = bits - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
}
int main() {
int num = 66; // Binary: 1000010
int bits = 8;
printf("Original number: %d
", num);
printf("Binary representation: ");
printBinary(num, bits);
printf("
");
// Two's complement using bitwise NOT and adding 1
int twosComp = (~num) + 1;
printf("Two's complement: ");
printBinary(twosComp & ((1 << bits) - 1), bits);
printf("\nDecimal value: %d
", twosComp & ((1 << bits) - 1));
return 0;
}
Original number: 66 Binary representation: 01000010 Two's complement: 10111110 Decimal value: 190
Key Points
- Two's complement is widely used to represent negative numbers in computers
- The operation involves finding one's complement first, then adding 1
- Bitwise NOT operator (~) can be used for one's complement on integers
- Always handle carry properly when adding 1 to avoid overflow
Conclusion
Two's complement is essential for representing negative numbers in binary systems. The process involves inverting all bits (one's complement) and adding 1, which can be implemented using string manipulation or bitwise operations in C.
Advertisements
