More bc weirdness

Actually no.

It assumes that any single letter that could be a hex number is one.

But in numbers with multiple digits, it interprets letters as 9’s.

Yes, 9’s.

The full story is a little more complicated.

bc will work in multiple bases, and it lets you set the input and output bases with the variables ibase and obase respectively.

Both are set to 10 by default.

When a number contains multiple characters, letters less than ibase are interpreted as you’d expect.

But letters greater than or equal to ibase are interpreted as ibase – 1.

So in base 12 in a number represented by more than one character, A means 10 and B means 11.

But C, D, E, and F also mean 11.

For example, A0 is 120 and BB is 143.

But CC is also 143.

If ibase is set to 10, then the expression E == F evaluates to false, because 14 does not equal 15.

But the expression expression EE == FF evaluates to true, because 99 equals 99.

If you set ibase to 16, then you’re in hex mode and the letters A through F behave exactly as expected.

If you want to go back to base 10, you need to set ibase to A, not 10.

If you’re in hex mode, every number you enter is interpreted in hex, and so “10” is interpreted as the number we usually write as 16.

In any base, setting ibase to 10 does nothing because it sets the base equal to the base.

.. More details

Leave a Reply