Binary To Decimal
Binary To Decimal
### Explanation:
- The function iterates through each digit of the binary number (starting from the
least significant bit) and converts it to its corresponding decimal value.
- It uses a `base` variable initialized to `1` (which represents \(2^0\)) and
multiplies it by 2 in each iteration to account for the increasing power of 2 as you
move to the next digit in the binary number.
1. **Initial Values:**
- `binary = 1101`
- `decimal = 0`
- `base = 1`
2. **First Iteration:**
- `lastDigit = binary % 10 = 1101 % 10 = 1`
- `decimal += lastDigit * base = 0 + 1 * 1 = 1`
- `base *= 2 = 1 * 2 = 2`
- `binary /= 10 = 1101 / 10 = 110`
3. **Second Iteration:**
- `lastDigit = binary % 10 = 110 % 10 = 0`
- `decimal += lastDigit * base = 1 + 0 * 2 = 1`
- `base *= 2 = 2 * 2 = 4`
- `binary /= 10 = 110 / 10 = 11`
4. **Third Iteration:**
- `lastDigit = binary % 10 = 11 % 10 = 1`
- `decimal += lastDigit * base = 1 + 1 * 4 = 5`
- `base *= 2 = 4 * 2 = 8`
- `binary /= 10 = 11 / 10 = 1`
5. **Fourth Iteration:**
- `lastDigit = binary % 10 = 1 % 10 = 1`
- `decimal += lastDigit * base = 5 + 1 * 8 = 13`
- `base *= 2 = 8 * 2 = 16`
- `binary /= 10 = 1 / 10 = 0`
6. **Termination:**
- The `while (binary > 0)` loop ends because `binary` is now `0`.
- The final `decimal` value is `13`.
### Result:
For the input `binary = 1101`, the function will return `13`, which is the decimal
equivalent of the binary number `1101`.
This function assumes that the binary number is passed as an integer with each
digit representing a bit (e.g., `1101` as a binary number).