Make a class in BaraBitKaar. Input two binary number. Sabtract two binary number.
BaraBitKaar Sabtract( BaraBitKaar b);
following example:
BaraBitKaar r;
r= p.sabtract(q); //this is how sabtract is supposed to be used to sabtract p and q. Notice r receives the return which is of type BaraBitKaar.
r.PrintBinary();
#include <iostream>
#include <bitset>
#include <iomanip>
using namespace std;
class BaraBitKaar{
string s;
unsigned long n;
public:
BaraBitKaar(string s){
this->s = s;
this->n = bitset<8>(this->s).to_ulong();
}
void printBinary(){
cout<<setw(9)<<this->s;
}
BaraBitKaar sabtract(BaraBitKaar b){
int n = this->n - b.n;
string r = bitset<8>(n).to_string();
return BaraBitKaar(r);
}
};
int main(){
string s1 = "1010101", s2 = "101011";
BaraBitKaar p(s1), q(s2);
BaraBitKaar r = p.sabtract(q);
cout<<setw(9)<<s1<<endl<<setw(1)<<"-"<<setw(8)<<s2<<endl;
r.printBinary();
return 0;
}
Comments
Leave a comment