Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the algorithm is about 2 numbers for example a,b in base 2 and adding them in base 2 without converting to base 10
and i've spend hours on it and there's only one day left

What I have tried:

this is the last thing i've tried:

c=0
i=1
ans=0
(**)c=c+a%10+b%10
if c>=2
{
ans=ans+10^i+(c-2)*10^(i-1)
c=1
}
else
{
ans=ans+c*10^(i-1)
c=0
}
a=a/10
b=b/10
i=i+1
if a=0 && b=0 && c=0
go (!!)
else
go(**)
(!!)end
Posted
Updated 24-Oct-17 12:01pm

1 solution

Do it the way processors do (sort of) - use a one-bit adder.
Extract each bit from a and b in turn, and add them together with a carry bit to produce a single bit solution with an updated carry:
Inputs:    Result:
A B C      C X
0 0 0      0 0
0 1 0      0 1
1 0 0      0 1
1 1 0      1 0
0 0 1      0 1
0 1 1      1 0
1 0 1      1 0
1 1 1      1 1


Use that it a loop to process all the bits, and you are done.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900