Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I read how to calculate using the command line calculator and a HERE-document, but nevertheless I do not get what I expected and can not find my error, what I did in the shell was:

bc << HERE
>ibase=2
>obase=16
>1001
>HERE
100

I expected to get 9 as result since binary 1001 is hexadecimal 9, but I got 100.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Because you set ibase=2 first, you need to use obase=10000:

$ echo 'ibase=2; obase=10000; 1001' | bc
9
share|improve this answer

Because you are setting the input base first, then when you set the output base, the 16 will be interpreted according to the input base (2). It appears that the 6 in 16 is simply interpreted is a binary 1 bit in this case, and so the output base gets set to binary 11 or decimal 3.

To work around this, you can set the output base before you set the input base:

echo 'obase=16; ibase=2; 1001' | bc
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.