+ 2
Explanation to a code for binary addition?
I've been doing some Java exercises online to improve. And I've come across this code for binary addition. But, I can't for the life of me figure out what it does. Would anyone happen to understand and be willing to explain it to this Java amateur? Much appreciated! https://code.sololearn.com/ctLuXZ57DyFg/?ref=app
5 Answers
0
binary1=1010
binary2=10010
so alike summing 2 numbers..
we go right to left
now both rightest digits are 0 and 0
for getting access of last digit we use remainder of division with 10
set a variable remainder = 0
now ;
(binart1%10 + binary2%10 + remainder)%2 = 1st sum of right most digits...
set
remainder=(binart1%10 + binary2%10)/2
for accessing 2nd rightest pair of digits
we 1st set binary1=binary1/10 &
binary2=binary2/10
**** this thing is in while loop--repeating upto binary1 and binary2 are non zero*****
as ;
this above is binary addition...
but compiler only understands normal addition...
what we do is below...
0+0=0-->0%2=0
1+0=1-->1%2=1
0+1=1-->1%2=1
1+1=2-->2%2=0 ( remainder is 1 )
thus we get binary adition...
+ 1
Thank you for your help! I'm fairly certain I understand now. I appreciate you taking your time to answer my question.
Have a great day!
0
10010
+1 010
-------
1 1100
binary addition...
google binary addition and see the mathematical method first...
its just that implementation of that method in java...
0+0=0
1+0=1
0+1=1
1+1=0 ( carry 1 )
0
Thank you for your answer.
I am aware of that, but the code's process is what I unfortunately dont understand. (The steps and logic behind it)
- 1
welcome brother..
not a problemâ