Suanovile
Suanovile

Reputation: 41

Algorithm for converting decimal to binary

I am having some difficulty understanding why this recursive algorithm works mathematically for converting decimal to binary.

I understand that the last digit in binary code determines if the decimal number is odd or even, thus we can use the remainder(%) to determine if the last digit is 0(odd) or 1(even). However, my thought process stops there, and I am confused about why the digit in position for 2 to the higher power can be calculated by dividing the decimal by 2 and then check its %2 as shown in the picture.

Can someone help me understand the logic behind the conversion? Thank you very much!

Decimal to Binary

Upvotes: 2

Views: 901

Answers (1)

Antonio Gargano
Antonio Gargano

Reputation: 21

This is the way to calculate a number in accord to a base, you can do it for every base b>1. for example the same number should be expressed in base b = 3

Start Conversion from base 10 to base 3

26/3 = 8 + 2
8/3 = 2 + 2
(26)_10 = (222)_3 
Proof
2*3^2 + 2*3 + 2*1 = 18+6+2 = 26

Upvotes: 2

Related Questions