+ All Categories
Home > Documents > Adding numbers without using the + operator

Adding numbers without using the + operator

Date post: 02-Jul-2015
Category:
Upload: kiwipom
View: 100 times
Download: 0 times
Share this document with a friend
17
Adding Numbers
Transcript
Page 1: Adding numbers without using the + operator

Adding Numbers

Page 2: Adding numbers without using the + operator

High Level Languages

• Java

• C#

• Haskell

Page 3: Adding numbers without using the + operator

High Level Languages

• c

* seriously

Page 4: Adding numbers without using the + operator

Syntactic Sugar

• + operator

Page 5: Adding numbers without using the + operator
Page 6: Adding numbers without using the + operator

Bitwise Operators

• ‘AND’ - takes two binary inputs. Returns 1 if both are 1, otherwise 0

0  and  0  -­‐-­‐>  0  0  and  1  -­‐-­‐>  0  1  and  0  -­‐-­‐>  0  1  and  1  -­‐-­‐>  1

Page 7: Adding numbers without using the + operator

Bitwise Operators

• ‘XOR’ - Exclusive OR. Returns 1 if one or the other inputs is 1, but not neither and not both.

0  xor  0  -­‐-­‐>  0  0  xor  1  -­‐-­‐>  1  1  xor  0  -­‐-­‐>  1  1  xor  1  -­‐-­‐>  0

Page 8: Adding numbers without using the + operator

Bit Shifting

• Left-shift - moves all the 1’s across to the left

       0001  0110    (22)  -­‐>    0010  1100    (44)  

• Right-shift - moves all the 1’s across to the right

       0001  0110    (22)  -­‐>    0000  1011    (11)  

Page 9: Adding numbers without using the + operator

The Formula

• Start with two numbers: x and y  

set a  =  (x  AND  y) set b  =  (x  XOR  y) set x  =  a left-shifted by 1 set y  =  b -- keep going until a is 0. The answer is b

Page 10: Adding numbers without using the + operator

E.G. 10 + 7

   1010  &  0111      -­‐-­‐-­‐-­‐      0010  -­‐>  Left-­‐shifted  -­‐>  0100  (4)  

   1010  ^  0111      -­‐-­‐-­‐-­‐      1101  (13)  

Page 11: Adding numbers without using the + operator

E.G. 4 + 13

   0100  &  1101      -­‐-­‐-­‐-­‐      0100  -­‐>  Left-­‐shifted  -­‐>  1000  (8)  

   0100  ^  1101      -­‐-­‐-­‐-­‐      1001  (9)  

Page 12: Adding numbers without using the + operator

E.G. 8 + 9

   1000  &  1001      -­‐-­‐-­‐-­‐      1000  -­‐>  Left-­‐shifted  -­‐>  10000  (16)  

   1000  ^  1001      -­‐-­‐-­‐-­‐      0001  (1)  

Page 13: Adding numbers without using the + operator

E.G. 16 + 1

   10000  &  00001      -­‐-­‐-­‐-­‐      00000  -­‐>  Left-­‐shifted  -­‐>  00000  (0)  

   10000  ^  00001      -­‐-­‐-­‐-­‐      10001  (17)  

Page 14: Adding numbers without using the + operator

Adding Numbers in C

int  add(int  x,  int  y)  {      int  a,b;      do  {          a  =  x  &  y;          b  =  x  ^  y;          x  =  a  <<  1;          y  =  b;      }  while  (a);      return  b;  }  

Page 15: Adding numbers without using the + operator

recursion!

int  add(int  x,  int  y)  {      int  a,b;      if  (x  ==  0)          return  y;  

   a  =  (x  &  y)  <<  1;      b  =  x  ^  y;      return  add(a,  b);  }

Page 16: Adding numbers without using the + operator

recursion!

int  add(int  x,  int  y)  {      if  (x  ==  0)          return  y;  

   return  add((x  &  y)  <<  1,  x  ^  y);  }

Page 17: Adding numbers without using the + operator

just for fun...

import  Data.Bits  

add  ::  (Bits  a)  =>  a  -­‐>  a  -­‐>  a  add  a  b      |  a  ==  0        =  b      |  otherwise  =  add  ((a  .&.  b)  `shiftL`  1)  (a  `xor`  b)  

https://gist.github.com/IanRandall/6775842


Recommended