Advertisement
sushmoyr

Binary Subtraction

Jul 22nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. //function for Binary Subtraction
  3. int binSubtracton(int a, int b)
  4. {
  5.       int carry;
  6.       //get 2's compliment of b and add in a
  7.       b = binAddition(~b, 1);
  8.  
  9.       while (b != 0) {
  10.               //find carry and shift it left
  11.               carry = (a & b) << 1;
  12.               //find the sum
  13.               a = a ^ b;
  14.               b = carry;
  15.       }
  16.       return a;
  17. }
  18.  
  19.  
  20. int main()
  21. {
  22.     int a,b;
  23.     cin>>a>>b;
  24.     cout<<binSubtracton(a,b)<<endl;
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement