0% found this document useful (0 votes)
5 views2 pages

Binary To Decimal

The function `binaryToDecimal(int binary)` converts a binary number represented as an integer into its decimal equivalent by iterating through each digit and calculating its value based on its position. For example, the binary number `1101` is converted to the decimal value `13`. The function works for any binary integer input, correctly producing the corresponding decimal output.

Uploaded by

prakash mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Binary To Decimal

The function `binaryToDecimal(int binary)` converts a binary number represented as an integer into its decimal equivalent by iterating through each digit and calculating its value based on its position. For example, the binary number `1101` is converted to the decimal value `13`. The function works for any binary integer input, correctly producing the corresponding decimal output.

Uploaded by

prakash mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

The function `binaryToDecimal(int binary)` converts a binary number

(represented as an integer) into its decimal equivalent.

### 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.

### Example Calculation:


Let's go through an example where `binary = 1101`:

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`.

### General Understanding:


- This function correctly converts any binary number (represented as an integer)
into its decimal form. For example:
- Input: `binary = 1010`, Output: `decimal = 10`
- Input: `binary = 111`, Output: `decimal = 7`
- Input: `binary = 10001`, Output: `decimal = 17`

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).

You might also like