Bitwise Operations by Ethr
Addition
a+b with bitwise operators recursively...
function add(a,b)
- result := a XOR b
- if a AND b > 0
- result:= add(result, (a AND b) << 1)
- return result
with a loop
c:= a AND b
result:= a XOR b
while c > 0
c:= c << 1
result := result XOR C
return result
Multiplication
lets say you want to do a*b using bit wise operators...
outline:
- set result to 0
- while 'b' isn't 0
- if LSB in 'b' is 1
- add 'a' to result
- a = a*2
- b = b*2
- return result
a+b with bitwise operators recursively...
function add(a,b)
- result := a XOR b
- if a AND b > 0
- result:= add(result, (a AND b) << 1)
- return result
with a loop
c:= a AND b
result:= a XOR b
while c > 0
c:= c << 1
result := result XOR C
return result
Multiplication
lets say you want to do a*b using bit wise operators...
outline:
- set result to 0
- while 'b' isn't 0
- if LSB in 'b' is 1
- add 'a' to result
- a = a*2
- b = b*2
- return result
