×

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

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 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.