Go back to previous page
Forum URL: http://www.cut-the-knot.org/cgi-bin/dcforum/forumctk.cgi
Forum Name: College math
Topic ID: 245
Message ID: 2
#2, RE: Binary system
Posted by RicBrad on May-15-02 at 06:40 PM
In response to message #0
The method I use to convert to a different base "b" (e.g. b=2 for binary) is using remainders and integer division.

If you use % for remainder (mod) and / for integer divison:
so 67 % 3 = 1 means "the remainder you get when dividing 67 by 3 is 1"
and 67 / 3 = 22 means "67 divided by 3 is 22 if you ignore remainders"
Then the most general method of converting is this:

Take your intial number N. Define the function f that takes a number and returns its digits, base b.
The first (least significant) digit in base b is (N % b).
The subsequent digits are given in the same way using f(N / b)
This is a recursive definition. Lets have an example:
in base 2 using , to separate the digits:
f(65) = f(65/2),(65%2)
= f(32),1 = f(32/2),(32%2),1 = f(16),0,1 = f(16/2),(16%2,0,1
=...=f(0),1,0,0,0,0,0,1
so f(65) = 01000001

and in answer to your first question the ASCII code for "A" is 65. So in the right context
A=01000001
but remember that this is simply a way of interpreting numbers as letters. The number 65 does not inherently equal "A".