SHARE
TWEET

Binary Subtraction

sushmoyr Jul 22nd, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top