Code to convert a binary number to decimal

I have some code that I wrote that will successfully return me a binary number. For example, running the code below with input 101 will return 5. However, the problem occurs when I add 0 bits to the left of the MSB, thus not changing the value. When I enter 0101 into the system, I should expect 5 to be returned again, but will return 17 instead.

Here is my code:

int dec1 = 0, rem1=0, num1, base1 = 1;

int a = 101;
while (a > 0){

    rem1 = a % 10;
    dec1 = dec1 + (rem1 * base1);
    base1 = base1 * 2;
    a = a / 10;
}
cout << dec1 << endl;

      

The result is 5. That's right.

However, when "a" is changed to 0101, the output will be 17. I believe my mistake is due to not understanding the modulo operator.

101% 10 = 1 correct? Does the compiler usually read 0101% 10 the same way?

I added a cout statement to my code to see what value is stored in rem1 after calculating the value 0101% 10.

int dec1 = 0, rem1=0, num1, base1 = 1;

int a = 101;
while (a > 0){

    rem1 = a % 10;
    cout << rem1 << endl;
    dec1 = dec1 + (rem1 * base1);
    base1 = base1 * 2;
    a = a / 10;
}
cout << dec1 << endl;

      

From this I was able to make sure that immediately after calculating 0101% 10, the value 5 is stored in rem1 instead of 1.

Does this add 0 before the MSB compiler "hey, is that a number in binary?" because if the compiler reads 5% 10 instead of 0101% 10, then I think the error makes sense.

After testing my theory, I changed the "a" to 1000 and the result was 8, which is correct.

Changing "a" to 01000 yields 24. rem1 = 01000% 10 should be 0, however rem1 stores 2. 01000 binary = 8 decimal. 8% 10 = 8? not 2?

I'm not sure what's going on and any help is appreciated!

+3


source to share


2 answers


101

parsed as a decimal (base 10) number, so you get the expected result.

0101

parsed as an octal (base 8) number because of the leading zero. A leading zero works the same as a prefix 0x

, which denotes a hexadecimal (base 16) number, except that without x

it is base 8 instead of base 16.

101 8= 8 2 + 8 0 = 64 + 1 = 65

65% 10 = 5

65/10 = 6

6% 10 = 7



5 * 2 + 7 = 17

If I were you, I would add assert(rem1 == 0 || rem1 == 1)

inside your loop right after your assignment to rem1

as a sanity check. If you ever get a remainder greater than one or less than zero, then obviously something is wrong.

As rbaleksandar points out in his comment above, the easiest way to avoid this problem is probably to store your input as a c-string ( char[]

) rather than using an integer literal. It's also good, because you can just iterate over characters to calculate the value instead of performing the %

and operations /

.

Alternatively, you can use hexadecimal literals (like 0x101

or 0x0101

) for all your inputs and change your math to use base 16 instead of base 10. This has the added advantage that base 10 division and the rest of the functions can be optimized by the compiler to many cheaper ones bit-shift and bit-mask operations, since 16 is cardinality 2. (For example, 0x101 % 16

==> 0x101 & 15

and 0x101 / 16

==> 0x101 >> 4

).


For more information see http://en.cppreference.com/w/cpp/language/integer_literal

+4


source


0101 is an octal number that has a value of 17.



0


source







All Articles