Write a program make a class barabitkaar.Given two arrays A[n] & B[n] containing bit strings representing sets A & B respectively, write a code fragment to construct bit string representing the set 𝐴 − 𝐵.
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string a;
std::string b;
cout << "Enter first bit string A : ";
cin >> a; int n = a.length();
std::cout << "Enter the second bit string B of length " << n<<" ";
cin >> b;
std::string result{ "" };
int delta = 0;
char mn;
for (int i = n - 1; i >-1; i--) {
mn = a[i];
if (delta == 1 && a[i] == '1')
{
mn = '0';
delta = 0;
}
if (delta == 1 && a[i] == '0')
{
mn = '1';
delta = 1;
}
if (mn =='0' && b[i]=='0')
{
result = "0" + result;
}
if (mn == '1' && b[i] == '0')
{
result = "1" + result;
}
if (mn == '1' && b[i] == '1')
{
result = "0" + result;
}
if (mn == '0'&& b[i] == '1')
{
result = "1" + result;
delta = 1;
}
}
if (delta == 1)
{
cout << "Negative binary number in two's complement format ";
}
cout << "A-B = "<<result<<endl;
return 0;
}
Comments
Leave a comment