Q:

Write a program in C++ to find two's complement of a binary number

0

Write a program in C++ to find two's complement of a binary number

Sample Output:

 Find two's complement of a binary value:                              
----------------------------------------------                         
 Input a 8 bit binary value: 01101110                                  
 The original binary = 01101110                                        
 After ones complement the value = 10010001                            
 After twos complement the value = 10010010

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
#define SZ 8
using namespace std;
int main()
{
    char bn[SZ + 1], onComp[SZ + 1], twComp[SZ + 1];
    int i, carr = 1;
    int er = 0;
    cout << "\n\n Find two's complement of a binary value:\n";
    cout << "----------------------------------------------\n";
    cout << " Input a " << SZ << " bit binary value: ";
    cin >> bn;
    for (i = 0; i < SZ; i++) 
    {
        if (bn[i] == '1') 
        {
            onComp[i] = '0';
        }
        else if (bn[i] == '0') 
        {
            onComp[i] = '1';
        }
        else 
        {
            cout << "Invalid Input. Input the value of assign bits." << endl;
            er = 1;
            break;
        }
    }
    onComp[SZ] = '\0';

    for (i = SZ - 1; i >= 0; i--) 
    {
        if (onComp[i] == '1' && carr == 1) 
        {
            twComp[i] = '0';
        }
        else if (onComp[i] == '0' && carr == 1) 
        {
            twComp[i] = '1';
            carr = 0;
        }
        else 
        {
            twComp[i] = onComp[i];
        }
    }
    twComp[SZ] = '\0';
    if (er == 0) 
    {
        cout << " The original binary = " << bn << endl;
        cout << " After ones complement the value = " << onComp << endl;
        cout << " After twos complement the value = " << twComp << endl;
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now