Home > Backend Development > C++ > body text

Find the 2's complement of a given binary number using C

PHPz
Release: 2023-09-05 15:21:06
forward
1196 people have browsed it

Find the 2s complement of a given binary number using C

Consider the example given below -

Example

Enter as follows:

Enter the binary number : 10010001

The output is as follows:

1 The complement of 10010001 is 01101110

2 The complement of 10010001 is 01101111

Algorithm

Refer to an algorithm to find the 2'c complement of a given binary number.

Step 1 - Get Started.

Step 2 - Read the binary number at runtime.

Step 3 - Copy the binary number to strdp.

Step 4 - len: = strlen(str)

Step 5 - For i = 0 to len-1 execute

Step 5.1 - If str[i] == '1' then

Step 5.1.1 - str[i] == '0'

Step 5.2 - Otherwise

Step 5.2.1 - str[i ] == '1'

Step 5.3 - i: = i 1

Step 5.3 - i: = i 1

p>

Step 6 - Mask Code: = 1

Step 7 - For i: = len-1 to 0 execute

Step 7.1 - If mask == 1 then p>

Step 7.1.1 − If str[i] == '1' then

        Step 7.1.1.1 − str[i]: = '0'

            Step 7.1.1.2 − mask: = 1

Step 7.1.2 − else

Step 7.1.2.1 − str[i]: = '1'

Step 7.1.2.2 - Mask: = 0

Step 7.1.3 - End if

Step 7.2 - End if

Step 8 - Print 2's complement.

Step 9 - Stop.

Program

The following is a C program to find the 2'c complement of a given binary number -

Live Demonstration

#include <string.h>
#include<stdio.h>
main(){
   char str[32],strdp[32];
   int mask,i;
   printf("Enter a binary number:");
   scanf("%s",str);
   strcpy(strdp,str);
   for(i=0;i<strlen(str);i++) /* computing 1&#39;s complement */{
      if(str[i]==&#39;1&#39;)
         str[i]=&#39;0&#39;;
      else
         str[i]=&#39;1&#39;;
   }
   printf("1\&#39;s complement of %s is %s</p><p>",strdp,str);
   mask=1;
   for(i=strlen(str)-1;i>=0;i--){
      if(mask==1){
         if(str[i]==&#39;1&#39;){
            str[i]=&#39;0&#39;;
            mask=1;
         }
         else{
            str[i]=&#39;1&#39;;
            mask=0;
         }
      }
   }
   printf("2\&#39;s complement of %s is %s",strdp,str);
}
Copy after login

Output

When the above program is executed, the following results are produced-

Enter a binary number:11001110
1&#39;s complement of 11001110 is 00110001
2&#39;s complement of 11001110 is 00110010
Copy after login

The above is the detailed content of Find the 2's complement of a given binary number using C. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!