Converting a number from decimal base to binary base (with the pencil)

This is a discussion on Converting a number from decimal base to binary base (with the pencil) within the C++ Programming forums, part of the General Programming Boards category; Hi to all! This is my first post so I feel like tot tell you that I studied programming in ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Converting a number from decimal base to binary base (with the pencil)

    Hi to all!
    This is my first post so I feel like tot tell you that I studied programming in HighSchool and after two years of Mechanical University I found a part time job that choosed me to do some things.
    I don't know C to much because I forgot most of it and actually I studyed it too little in the HighSchool (I didn't know I was wasting my time ) and I have some mounths to study it and do the job.

    To get to the subject, I want to know the method to transform a decimal number in a binary number, but I want to know it like you write it on a piece of paper.
    I kind of remember something and it's like this:

    -for exemple to transform number 8810


    --------------88/2
    --------------88|44/2
    ---------------0|44|22/2
    ------------------0|22|11/2
    ---------------------0|10|5/2
    ------------------------1|4|2\2
    --------------------------- 1|2|1
    ------------------------------0


    I will explain what I did:
    -first line I divided 88 by 2;
    -44 of the second line is the result of 88 divided by 2;
    -88 of the second line is the result of the multiplication of 44 with the same 2;
    -0 of the third line is the result of the difference between 88 of the first line and 88 of the second line;
    -44 of the second line I divided it by 2;
    -forward I followed the same algoritm;

    The binary number of 8810 is the red number read from right to left: 10110002 .

    My problem is that I don't know if the algoritm is correct, so I ask for help.

    Thank you for reading it.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    well if you learn your base 2 numbers it is easier. Instead of 88/2 you can just find the closes number close to 88 in base 2. Then subtract that number from 88, then get whatever's left, and find the closest base 2 number close to that, until you get the decimal sequence in conjunction with the exponents of base 2.

    Example

    88
    64 <--- 2^6 closest number to 88 in base 2
    ---
    24
    16<----2^4
    ---
    8<-------2^3
    8
    ----
    0

    Now decimal places 6, 4, and 3 are all 2s.

    1011000
    Last edited by indigo0086; 10-06-2006 at 09:40 AM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Quote Originally Posted by indigo0086
    Now decimal places 6, 5, and 3 are all 1s.

    1011000
    6, 5 and 3?
    I used to be an adventurer like you... then I took an arrow to the knee.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    6, 4, and three. Sorry for my dyslexia.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Now decimal places 6, 4, and 3 are all 2s.
    All 2s?



    [edit] OK, really, though, the algorithm works, I think. This is the way I learned it, which is probably just the same, but worded a bit differently:
    Code:
    0  88
    0 44
    0 22
    1 11
    1 5
    0 2
    1 1
       0
    First, 88 divided by 2 is 44 remainder 0. 44 divided my 2 is 22 remainder 0, and so on down the line. (I would include them all, but all this coloring is really tedious.) Once you hit 0 on the right column, you are done, since 0 divided by 2 is 0 remainder 0, which you could just keep doing all day long if you keep going. You get your answer by writing the remainders in the opposite order you got them:
    Code:
    1011000
    or 1011000 if the colors make your eyes hurt. (And let's face it; they do.)
    Last edited by Decrypt; 10-06-2006 at 10:35 AM.
    There is a difference between tedious and difficult.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    "I am Error"

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    128 64 32 16 8 4 2 1

    88 >= 128 ? 0
    88 >= 64 ? 1 (88-64 = 24)
    24 >= 32 ? 0
    24 >= 16 ? 1 (24 - 16 = 8)
    8 >= 8 ? 1 (8 - 8 = 0)
    0 >= 4 ? 0
    0 >= 2 ? 0
    0 >= 1 ? 0

    0101 1000
    Last edited by simpleid; 10-06-2006 at 11:02 AM.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Thank you indigo0086, I've got the algoritm.
    Decrypt, your algoritm is exactly like my algoritm but I write more of it. It's simpler like yours.
    simpleid, why is that a zero before all the number because all have not got that zero?

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Square root algorithm is much more fun!
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    simpleid, why is that a zero before all the number because all have not got that zero?
    He's padding the number out to 8 digits, or one byte.

    0100 = 100.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary to decimal
    By yigster in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 04:00 AM
  2. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 08:51 PM
  3. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 03:31 AM
  4. Printing total of 1's in binary number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 01:50 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21