Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Base 10 to Base 2  (Read 15159 times)

0 Members and 1 Guest are viewing this topic.

JJ 3000

    Topic Starter


    Egghead
  • Thanked: 237
  • Experience: Familiar
  • OS: Linux variant
Base 10 to Base 2
« on: March 16, 2010, 08:42:47 PM »
What's the easiest formula to use to convert base 10 to base 2?
Save a Life!
Adopt a homeless pet.
http://www.petfinder.com/

Salmon Trout

  • Guest
Re: Base 10 to Base 2
« Reply #1 on: March 17, 2010, 01:37:51 PM »
Comparison with descending powers of two and subtraction

Let's convert the decimal number 156 to binary. What is the greatest power of two that will fit into 156? Since 128 fits, write a 1 for the leftmost binary digit, and subtract 128 from your decimal number, 156. You now have 28.

Move to the next lower power of two. Can 64 fit into 28? No, so write a 0 for the next binary digit to the right.

Can 32 fit into 28? No, so write a 0.

Can 16 fit into 28? Yes, so write a 1, and subtract 16 from 28. You now have 12.

Can 8 fit into 12? Yes, so write a 1, and subtract 8 from 12. You now have 4.

Can 4 (power of two) fit into 4 (working decimal)? Yes, so write a 1, and subtract 4 from 4. You have 0.

Can 2 fit into 0? No, so write a 0.

Can 1 fit into 0? No, so write a 0.

Since there are no more powers of two left, you are done. You should have 10011100. This is the binary equivalent of the decimal number 156.

 The answer is the remainders read from the bottom up.

umbra



    Beginner
  • Thanked: 2
    • Experience: Familiar
    • OS: Windows 7
    Re: Base 10 to Base 2
    « Reply #2 on: March 17, 2010, 02:58:49 PM »
    Hi,

    You could try this example , that algorithm is the best in my opinion:

    http://mathbits.com/mathbits/compsci/Introduction/frombase10.htm

    Something like : 31 is ?

    31 : 2 = 15 r 1
    15 : 2 = 7 r 1
    7 : 2 = 3 r 1
    3 : 2 = 1 r 1
    1 : 2 = 0 r 1

    So 31 = 11111 ( hmmm one more example , a better example could be... shown)

    Let's say.... 20

    20 : 2= 10 r 0 (20 = 2 * 10 + 0 )
    10 : 2= 5 r 0  ( 10 = 2*5  + 0 )
    5 : 2 = 2 r 1   ( 5 = 2*2 + 1 )
    2 : 2 = 1 r 0   ( 2 = 2 * 1 + 0)
    1 : 2 =  0 r 1  ( 1 = 2*0 + 1 )

    So 20 = 10100

    Execute those divisions until 0 (as floor) appears in front of r , and your number is constructed from "r" part ( remainder) which should be read from bottom to top.

    In those link also this algorithm is explained , may be better how i do it. (any way this algorithm could be used for any number base conversion )

    Regards,

     


    Trying to don't waste my time.  :)

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Base 10 to Base 2
    « Reply #3 on: March 17, 2010, 03:10:23 PM »
    Easy? Use a calculator.
    http://www.tucows.com/preview/414618
    Or a table.
    http://www.ascii.cl/conversion.htm
    No formula is needed.
    Binary numbers has the same value as decimal numbers.

    OK. Just joking. But is seems rather silly to ask on a forum.
    Can't you do this is your head?
    1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 and so on.

    umbra



      Beginner
    • Thanked: 2
      • Experience: Familiar
      • OS: Windows 7
      Re: Base 10 to Base 2
      « Reply #4 on: March 17, 2010, 04:19:44 PM »
        In my opinion you should know how that algorithm works, so that if necessary you could program it,  and it will be computer's job to calculate,   ", 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 and so on." but you should know how  to tell it to do it.

       I believe that this question is really a good one.

      Trying to don't waste my time.  :)

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Base 10 to Base 2
      « Reply #5 on: March 17, 2010, 05:28:35 PM »
      Then let's clarify the question.
      An input source has some numbers in ASCII format that represent a  number using base ten, sometimes called decimal. We shall change this sequence of symbols into a binary number that represents the value of the visible symbols that most humans understand. (Assembly language programmers are almost human, but that is another topic.)

      What have to understand the visible representation of number ant the absolute binary value.

      In this example we suppose the the number does not have a decimal point or does not have anything to the right of the point. And the number is less that 32,768 if we want to limit this to a 16 bit operation.

      First, we chose a register or variable in memory large enough to hold the binary value. In this case a two byte memory or register.  We start with a value of zero. Next we start to the far left of the string of decimal symbols. We subtract 48 to convert the ASCII symbol to a corresponding absolute binary value. This will be done for each digit in the string. but before we do the cent digit, if there is another, we multiply our temp value by ten before we add the value of the current digit.

      Using a pseudo code, it might look link this.
      Code: [Select]
      set 16bit location to zero. call is result
      set a Bolean  to false and call it done.
      while NOT done
        get current digit, subtract 48, store in a temporary place. call it temp
        multiply result by ten
        add temp to result
        incitement digit pointer
        if pointer  beyond last digit then let done be true
      loop
      use result as binary value.

      Now then, do you want to represent the binary value as a string of 1s and 0s ?
      That's another exercise.

      But did you want to BCD? Packed or unpacked?

      Do you need to understand this? I don't know. You can go through life and be happy without needing to understand the reson roe using numbers systems other that base ten.

      We must understand, however you represent a number, it is a number and a number is number.
      for example the value fifteen and be:
      15 base 10
      0F base 16
      0000 1111 base two
      or even like this:
                             


      Fleexy



        Intermediate

      • OW NEXT TIME I SHOULD TURN IT OFF BEFORE SERVICING
      • Thanked: 2
        • Yes
        • Yes
      • Experience: Experienced
      • OS: Windows XP
      Re: Base 10 to Base 2
      « Reply #6 on: March 19, 2010, 05:05:19 PM »
      If you are using any fomr/mutation of BASIC you can use BIN$(x).

      So, to make a QuickBASIC base10 --> base2 program, use something like this:

      CLS
      INPUT "Enter base 10 number:", x
      PRINT "Binary form: " + BIN$(x)
      DO WHILE INKEY$ = ""
      LOOP
      END
      I love .NET!

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Base 10 to Base 2
      « Reply #7 on: March 20, 2010, 09:33:30 AM »
      If you are using any fomr/mutation of BASIC you can use BIN$(x).

      So, to make a QuickBASIC base10 --> base2 program, use something like this:

      CLS
      INPUT "Enter base 10 number:", x
      PRINT "Binary form: " + BIN$(x)
      DO WHILE INKEY$ = ""
      LOOP
      END


      "BIN$" or "BIN" functions do not exist in QBASIC, QuickBASIC, or any dialect of Visual Basic 1 through 6.

      It's probably exclusive to a single dialect, such as RealBasic.


      here are two functions that I never use that  Ifound in one of my modules (VB6):


      Code: [Select]
      Public Function BinaryToDecimal(ByVal BinValue As String) As Long
          Dim lngValue As Long
          Dim X        As Long
          Dim K        As Long
          K = Len(BinValue)                                                   ' will only work with 32 or fewer "bits"
          For X = K To 1 Step -1                                                   ' work backwards down string
              If Mid$(BinValue, X, 1) = "1" Then
                  If K - X > 30 Then                                                   ' bit 31 is the sign bit
                      lngValue = lngValue Or -2147483648# _
                                            ' avoid overflow error
                  Else
                      lngValue = lngValue + 2 ^ (K - X)
                  End If
              End If
          Next X
          BinaryToDecimal = lngValue
      End Function


      and the reverse:

      Code: [Select]
      Public Function dec2bin(mynum As Variant) As String
          Dim loopcounter As Integer
          If mynum >= 2 ^ 31 Then
              dec2bin = "Number too big"
              Exit Function
          End If
          Do
               
              If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
                  dec2bin = "1" & dec2bin
              Else
                  dec2bin = "0" & dec2bin
              End If
              loopcounter = loopcounter + 1
          Loop Until 2 ^ loopcounter > mynum
      End Function

      I was trying to dereference Null Pointers before it was cool.

      Fleexy



        Intermediate

      • OW NEXT TIME I SHOULD TURN IT OFF BEFORE SERVICING
      • Thanked: 2
        • Yes
        • Yes
      • Experience: Experienced
      • OS: Windows XP
      Re: Base 10 to Base 2
      « Reply #8 on: March 20, 2010, 05:09:17 PM »
      Oops, sorry, got it mixed up with HEX$ and OCT$.  Disregard my post please.

      ...probably because I made my own sub BIN$ (can't find it now, must be on my other computer)
      I love .NET!