×

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to 0 and 0 to 1; adding one to the 1s complement of a binary number produces the 2s complement of a binary number.

The binary number in the above figure is equal to 00010100, and its own complement is determined by converting the bit 1 to 0 and 0 to 1 vice versa. Therefore, the alternative to one is 11101011. After calculating one's add-on, we measure the two's add-on by adding 1 to one's add-on, and the result is 11101100.

Let's create a complement of 2s program.

#include <stdio.h> 
int main() 
{ 
   int n;  // variable declaration 
   printf("Enter the number of bits do you want to enter :"); 
   scanf("%d",&n); 
   char binary[n+1];  // binary array declaration;  
   char onescomplement[n+1]; // onescomplement array declaration  
   char twoscomplement[n+1]; // twoscomplement array declaration 
   int carry=1; // variable initialization 
   printf("\nEnter the binary number : "); 
   scanf("%s", binary); 
   printf("%s", binary); 
   printf("\nThe ones complement of the binary number is :"); 
   // Finding onescomplement in C 
   for(int i=0;i<n;i++)  
   { 
       if(binary[i]=='0') 
       onescomplement[i]='1'; 
       else if(binary[i]=='1') 
       onescomplement[i]='0'; 
   } 
   onescomplement[n]='\0'; 
   printf("%s",onescomplement); 
printf("\nThe twos complement of a binary number is : "); 
// Finding twoscomplement in C 
for(int i=n-1; i>=0; i--) 
    { 
        if(onescomplement[i] == '1' && carry == 1) 
        { 
            twoscomplement[i] = '0'; 
        } 
        else if(onescomplement[i] == '0' && carry == 1) 
        { 
            twoscomplement[i] = '1'; 
            carry = 0; 
        } 
        else 
        { 
            twoscomplement[i] = onescomplement[i]; 
        } 
    } 
twoscomplement[n]='\0'; 
printf("%s",twoscomplement); 
return 0; 
} 

Analysis of the above program,

First, we input the bits number, and it's stored in the 'n' variable.

We declare character array after entering the number of bits, i.e., char binary [n+1], which holds the binary number. The 'n' is the number of bits that we entered in the preceding step; it basically defines the array size.

We declare two more arrays, one complement[n+1] and the other twoscomplement[n+1], respectively. The onescomplement[n+1] array contains those that complement a binary number while the twoscomplement[n+1] array contains a binary number complement to the two.

Initialize the variable carrying, and assign 1 value to the variable.

We input the binary number after declarations.

Now, we actually calculate a binary number's complement to the one. To do this, for(int i=0;i < n;i++) we construct a loop that iters in the binary array. The condition is checked in for loop, whether the bit is 1 or 0. If the bit is 1 then add addition[i]=0 to supplement another[i]=1. In this way, a binary number complement to one is generated.

We produce the 2s complement of a binary number, after calculating one's complement. To do this, we build a loop that iters to the starting element from the last element. We have three conditions in the loop:

  • If the add-on bit[i] is 1 and the carry value is 1 then we put 0 in twocomplement[i].
  • If the add-on bit[i] is 0 and the carrying value is 1, then we put 1 in twoscomplement[i] and 0 in carrying.
  • If the above two conditions are wrong, then the addition[i] equals twoscomplement[i].

Related Topics

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.