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

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Question 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.
    Last edited by Muster Mark; 01-22-2011 at 04:14 PM. Reason: typos

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    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
    Last edited by Muster Mark; 01-22-2011 at 09:47 PM. Reason: typo

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Muster Mark View Post
    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...
    Last edited by CommonTater; 01-22-2011 at 10:52 PM.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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...

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... the only time it needs base 10 is when it's putting stuff on the screen.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by CommonTater View Post
    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...
    Devoted my life to programming...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sipher View Post
    and even then it isn't always useful...
    42 ... ? ... You gotta be kidding me!

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by CommonTater View Post
    42 ... ? ... You gotta be kidding me!
    ???

    EDIT: Oh, I get what you said now. Well, is this decimal? FF
    Last edited by GReaper; 01-25-2011 at 10:40 AM.
    Devoted my life to programming...

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    ???

    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!"

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    OOOOhhhhhh....
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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.

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by CommonTater View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM
  4. binary to decimal
    By jk81 in forum C Programming
    Replies: 1
    Last Post: 09-13-2002, 05:20 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM