Skip to main content

Posts

Showing posts from August, 2017

JavaScript: How to Convert One Number Base (Radix) to Another

Decimal to Binary For instance, we have number 7 , which is base-10 number. Then, we need to convert to base-2 (binary) system. The base-2 system uses the addition of power of 2's to get the number we have in another radix system. So to get the value from the other base system (base-10 for example), we continuously divide that number with 2, and take the remainder of each division . In paper and pen scribbling, we do this: 7 ÷ 2 ➡️ closest multiplier is 3, remainder 1 3 ÷ 2 ➡️ closest multiplier is 1, remainder 1 1 ÷ 2 ➡️ the only multiplier is 0, remainder 1 Put all remainders as a number . Take each from the BOTTOM : 1 ➡️ 1 ➡️ 1 . Meaning 7 in binary system is 111. $7_10 = 111_2$ Or, we could use the addition method of power of 2: $$ 7 = (\bo1 × 2^2) + (\bo1 × 2^1) + (\bo1 × 2^0)$$ Look at the multiplier (1), read it from the LEFT : 1 ➡️ 1 ➡️ 1 . Similar to prior method, 7 in base-10 is equal to 111 in base-2 system. $7_10 = 111_2$ JavaSc...