In this recipe, you will learn how to convert a binary number into a decimal.
Converting a binary number into a decimal using a bitwise operator
How to do it…
To convert a binary number into a decimal, perform the following steps:
- Enter a binary number.
- Apply a mod 10 (% 10) operator to the binary digits of the binary number to isolate the last bit of the binary number.
- Left-shift the binary digit isolated in step 2 to multiply it by the power of 2.Â
- Add the product of the previous multiplication to the variable that will store the result, that is, the decimal number. Let's call the variable dec.
- The last digit of the binary number is truncated.
- Repeat step 2 to step 4 until...