Advertisement
STANAANDREY

bitwise substract

Nov 1st, 2022
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.37 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. unsigned substract(unsigned a, unsigned b) {
  4.   while (b) {
  5.     unsigned borrow = ~a & b;
  6.     unsigned diff = a ^ b;
  7.     borrow <<= 1;
  8.     a = diff;
  9.     b = borrow;
  10.   }
  11.   return a;
  12. }
  13.  
  14. int main(void) {
  15.   unsigned a, b, diff;
  16.   scanf("%u", &a);
  17.   scanf("%u", &b);
  18.   diff = substract(a, b);
  19.   printf("%u-%u=%u\n", a, b, diff);
  20.   return 0;
  21. }
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement