Our professor threw this lab assignment at us:
Input an integer containing only 0s and 1s (i.e. as a "binary" integer) and print its decimal equivalent. (hint: Use the modulus and division operators to pick off the "binary" number's digits one at a time from right to left). Just as in the decimal number system where the rightmost digit has a positional value of 1, and the next digit left has a positional value of 10, then 100, then 1000, etc., in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, etc. Thus the decimal number 234 can be interprete4d as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.
We are on chapter three learning about control structures so we haven't gotten that far into the course. The main thing I don't understand is the part about "using the modulus and division operators to pick of the "binary" number's digits one at a time." It is pretty clear how you determine the decimal equivalent but what is the logic behind finding each digit's value? I have no clue how to write the code to find the value of each digit in the binary number 1101.
Does anyone have any ideas? I would appreciate any help.
Thanks,
Gage
Input an integer containing only 0s and 1s (i.e. as a "binary" integer) and print its decimal equivalent. (hint: Use the modulus and division operators to pick off the "binary" number's digits one at a time from right to left). Just as in the decimal number system where the rightmost digit has a positional value of 1, and the next digit left has a positional value of 10, then 100, then 1000, etc., in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, etc. Thus the decimal number 234 can be interprete4d as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.
We are on chapter three learning about control structures so we haven't gotten that far into the course. The main thing I don't understand is the part about "using the modulus and division operators to pick of the "binary" number's digits one at a time." It is pretty clear how you determine the decimal equivalent but what is the logic behind finding each digit's value? I have no clue how to write the code to find the value of each digit in the binary number 1101.
Does anyone have any ideas? I would appreciate any help.
Thanks,
Gage
