Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Bitwise subtraction in Python

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 0.57 KB  |  views: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<stdio.h>
  2.  
  3. int add(int x, int y) {
  4.     int a, b;
  5.     do {
  6.         a = x & y;
  7.         b = x ^ y;
  8.         x = a << 1;
  9.         y = b;
  10.     } while (a);
  11.     return b;
  12. }
  13.  
  14. int main( void ){
  15.     printf( "6 + 3 = %d", add(6,3));
  16.     printf( "6 - 3 = %d", add(6,-3));
  17.     return 0;
  18. }
  19.        
  20. def add(x, y):
  21.     while True:
  22.         a = x & y
  23.         b = x ^ y
  24.         x = a << 1
  25.         y = b
  26.         if a == 0:
  27.             break
  28.     return b
  29.  
  30. print "6 + 3 = %d" % add(6,3)
  31. print "6 - 3 = %d" % add(6,-3)
  32.        
  33. >>> 1 << 100
  34. 1267650600228229401496703205376L
  35.        
  36. x = (a << 1) & 0xffffffff
clone this paste RAW Paste Data