top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to print 1s and 2s complement of a Binary String using C?

+1 vote
919 views

A binary string will be given to us and we need to print its 1s and 2s complement of that? C code would be helpful?

posted Jan 29, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

one's complement of a binary number in C
Example:

Input binary number: 01000011 
Ones's complement: 10111100 

Required knowledge:
Basic C programming, For loop, String

One's complement
One's complement of a binary number is defined as value obtained by inverting all binary bits i.e. swapping all 1's to 0's and all 0's to 1's.

/**
 * C program to find 1's complement of a binary number
 */

#include <stdio.h>
#include <string.h>

#define SIZE 8

int main()
{
    char binary[SIZE + 1], onesComp[SIZE + 1];
    int i, error=0;

    /*
     * Reads binary value from user
     */
    printf("Enter any %d bit binary value: ", SIZE);
    gets(binary);

    /*
     * Stores all inverted bits of binary value to onesComp
     */
    for(i=0; i<SIZE; i++)
    {
        if(binary[i]=='1')
        {
            onesComp[i] = '0';
        }
        else if(binary[i]=='0')
        {
            onesComp[i] = '1';
        }
        else
        {
            printf("Invalid Input");
            error = 1;
            break;
        }
    }
    onesComp[SIZE] = '\0';

    if(error==0)
    {
        printf("\nOriginal binary = %s\n", binary);
        printf("Ones complement = %s", onesComp);
    }

    return 0;
}

Output:

Enter any 8 bit binary value: 00001111

Original binary = 00001111 
Ones complement = 11110000

2's complement of a binary number in C.

Example:
Input binary number: 0110 1110
Output 2's complement : 1001 0010

Required knowledge:
Basic C programming, For loop, String

Two's complement
Wikipedia states that the two's complement of an N-bit number is defined as the complement with respect to 2N; in other words, it is the result of subtracting the number from 2N, which in binary is one followed by N zeroes.

Or in simple words two's complement can be also defined as sum of 1's complement of the binary number and 1.

/**
 * C program to find 2's complement of a binary number
 */

#include <stdio.h>
#include <string.h>

#define SIZE 8

int main()
{
    char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
    int i, carry=1;

    /*
     * Reads binary number from user
     */
    printf("Enter any %d bit binary value: ", SIZE);
    gets(binary);

    /*
     * Finds the 1's complement of the binary number
     */
    for(i=0; i<SIZE; i++)
    {
        if(binary[i]=='1')
        {
            onesComp[i] = '0';
        }
        else if(binary[i]=='0')
        {
            onesComp[i] = '1';
        }
    }
    onesComp[SIZE] = '\0';

    /*
     * Adds 1 to the 1's complement of the binary number to get 2's complement
     */
    for(i=SIZE-1; i>=0; i--)
    {
        if(onesComp[i]=='1' && carry==1)
        {
            twosComp[i] = '0';
        }
        else if(onesComp[i]=='0' && carry==1)
        {
            twosComp[i] = '1';
            carry = 0;
        }
        else
        {
            twosComp[i] = onesComp[i];
        }
    }
    twosComp[SIZE] = '\0';

    printf("\nOriginal binary value = %s\n", binary);
    printf("One's complement = %s\n", onesComp);
    printf("Two's complement = %s", twosComp);

    return 0;
}

Output:

Enter any 8 bit binary value: 01101100 

Original binary value = 01101100
One's complement = 10010011
Two's complement = 10010100
answer Jan 30, 2016 by Shivaranjini
Similar Questions
+3 votes
0 votes

I am looking to reverse a string using binary search algorithm but no clue. Can someone share the algo and may be C/C++ code?

+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

–1 vote

Given a Singly linked list with each node containing either 0, 1 or 2. Write code to sort the list.
Input : 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> 0
Output : 0 -> 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2

...