Skip to content
Home » C Program To Convert Binary Number To 2’s Complement

C Program To Convert Binary Number To 2’s Complement

    A 2’s complement is used for binary operations in a computer system. In this example program, you are going to receive a binary string and convert the string into 2’s complement in two steps.

    Learn the basics of C programming before you begin with the example. Skip this step if you already familiar with these concepts.

    Problem Definition

    The program ask for a input binary string. To convert this binary string in to 2’s complement, you need two steps.

    1. Convert the input binary string to 1’s complement.
    2. Add 1 to the 1’s complement obtained.

    For example, suppose a the input string is 1100.

    Step 1: The 1’s complement can be obtained by converting 1’s in to 0 and 0s into 1s.

    \begin{aligned}1100 \hspace{3px}becomes \hspace{3px}0011\end{aligned}

    Step 2: Add 1 to the 1’s complement to get 2’s complement of the number.

    This step requires you to understand the binary math.

    \begin{aligned}&1 + 1 = 10\\&1 + 0 = 1\\&0 + 1 = 1\\&0 + 0 = 0\end{aligned}

    Using the above, you add 1 to the 1’s complement.

    \begin{aligned}&\hspace{2px}0\hspace{2px}0\hspace{2px}1\hspace{2px}1\\&\hspace{8px} + 1 \\&----\\&\hspace{2px}0\hspace{2px}1\hspace{2px}0\hspace{2px}0\\&----\end{aligned}

    Therefore,

    The two’s complement of the number is 0100.

    Flowchart

    Flowchart - Program for 2's Complement
    Flowchart – Program for 2’s Complement

    Program Source Code

    #include <stdio.h>
    #include <stdlib.h>
    void complement(char *a);
    int main()
    {
        char a[16];
        int i;
        printf("Enter the binary number:");
        gets(a);
    for(i=0;a[i] != '\0';i++)
    {
        if(a[i] != '0' && a[i]!= '1')
        {
        printf("The number entered is not a binary number"); 
        printf("Enter the correct number!");
        exit(0);
        }
    }
    complement(a);
    getch();
    system("PAUSE"); 
    return 0;
    }
    
    void complement (char *a)
    {
        int l, i, c = 0;
        char b[16];
        l = strlen(a);
    /* Get the 1's complement of the given binary string */
        for(i = l-1;i>=0;i--)
        {
            if(a[i] == '0')
            {
            b[i]= '1';
            }
            else
            {
            b[i] = '0';
            }
        }
    /* Add 1 to the 1's complement obtained in the previous step */
        for(i = l-1;i>=0;i--)
        {
        if(i == l-1)
        {
            if(b[i]== '0')
            {
            b[i] = '1';
            }
            else
            {
            b[i] == '0';
            c = 1;
            }
        }
        else
        {
            if(c==1 && b[i] == '0')
            {
            b[i] = '1';
            c = 0;
            }
            else if(c == 1 && b[i] == '1')
            {
            b[i] = '0';
            c = 1;
            }
        }
        }
    b[l] = '\0';
    printf("The 2's Complement is %s\n", b);
    }

    Output

    Enter the binary number:11000101
    The 2's Complement is 00111011

    The given input string here is 11000101 and the final output is its 2’s complement.

    \begin{aligned}11000101  \hspace{2px} has\hspace{2px}  1's \hspace{2px} complement \hspace{2px} of\hspace{2px}  00111010\end{aligned}

    And if you add 1 to 1’s complement.

    \begin{aligned}00111010 + 1 \rightarrow 00111011\end{aligned}

    Therefore, the 2’s complement is 00111011.