Advertisement
Guest User

Bitwise subtraction in Python

a guest
Feb 27th, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement