# How does a computer do base conversion (binary <-> decimal)?

• 01-22-2011
Muster Mark
How does a computer do base conversion (binary <-> decimal)?
So, I know how to convert between bases, but I was wondering how the computer implements base conversion for I/O (I assume this is done by the OS?).

I can think that for relatively small numbers the conversion is fairly simple, but when I give the computer 18446744073709551615 as numeric input, it knows to store it as 11111111111111111111111111111111111111111111111111 11111111111111, and then display the decimal again when asked to interpret the bits as an integer. The only algorithms I know would get really slow with such large numbers, so how is the computer doing this?

I started to think about this because I was wondering how to implement a program that would read a binary file and print it as a huge decimal number (just for giggles, I guess).

Thanks for any insight.
• 01-22-2011
CommonTater
Actually number base conversions aren't usually part of the OS... it just runs code...

Most often it's done in libraries and sometimes in compilers...

They probably use about the same methods we would, except in a highly optimzed form, doing register math and fast bit shifting, right in the CPU.
• 01-22-2011
Muster Mark
Hey, thanks for the response. What you say makes sense. So, I guess there is no easy way to convert, for example, 2^(2^32) into base ten, at least not an obvious one. I will have to tackle it as an interesting Math problem, me thinks!

Cheers
• 01-22-2011
CommonTater
Quote:

Originally Posted by Muster Mark
Hey, thanks for the response. What you say makes sense. So, I guess there is no easy way to convert, for example, 2^(2^32) into base ten, at least not an obvious one. I will have to tackle it as an interesting Math problem, me thinks!

Cheers

Think about the base 10 value of each bit...

Then think about how you might make a real simple loop to add up the values of only the 1 bits...
• 01-24-2011
zacs7
How slow do you think it's going to get? Decimal is really only a nicety for humans. You aren't going to be converting from base-2 to base-10 on every clock tick...
• 01-24-2011
CommonTater
Yep... the only time it needs base 10 is when it's putting stuff on the screen.
• 01-24-2011
GReaper
Quote:

Originally Posted by CommonTater
Yep... the only time it needs base 10 is when it's putting stuff on the screen.

And even then it isn't always useful...
• 01-24-2011
CommonTater
Quote:

Originally Posted by sipher
and even then it isn't always useful...

42 ... ? ... You gotta be kidding me!
• 01-25-2011
GReaper
Quote:

Originally Posted by CommonTater
42 ... ? ... You gotta be kidding me!

??? :confused:

EDIT: Oh, I get what you said now. Well, is this decimal? FF
• 01-25-2011
CommonTater
Quote:

Originally Posted by Sipher
??? :confused:

EDIT: Oh, I get what you said now. Well, is this decimal? FF

No, it's a reference to "The Hitchhiker's Guide To The Galaxy"... a movie. The sub-plot referenced is where a race of infinitely curious superbeings commissioned a massive computer and tasked it with finding "The answer to love, life, and everything else"... The computer worked on this for a million years and when they returned, the computer anounced that the answer to "love, life and everything else" is 42... One of the superbeings snapped back "42 ... You've got to be kidding me!"
• 01-26-2011
GReaper
OOOOhhhhhh....
• 01-26-2011
nonoob
If you wanted to write a binary-to-decimal converter that handles huge numbers, that would be a fun project. I can't think of any super clever algorithm, except ones that loop simply to generate each decimal digit. It would be a gigantic divide-by-10 deal that strips of the least significant units digit in each loop.
• 01-26-2011
fronty
Quote:

Originally Posted by CommonTater
No, it's a reference to "The Hitchhiker's Guide To The Galaxy"... a movie. The sub-plot referenced is where a race of infinitely curious superbeings commissioned a massive computer and tasked it with finding "The answer to love, life, and everything else"... The computer worked on this for a million years and when they returned, the computer anounced that the answer to "love, life and everything else" is 42... One of the superbeings snapped back "42 ... You've got to be kidding me!"

In fact it is originally a radio comedy by Douglas Adams, but it's better known (at least in Finland and as far as I know in other parts of world) as a book, which is part of Adams book series, which is world's only five-part trilogy. I don't know if the movie includes the love, but in the books and Finnish version of the radio comedy call it the answer to the ultimate question to life, universe and everything.