[Tutor] Need help with binary notation

Lloyd Hugh Allen lha2@columbia.edu
Thu, 20 Jun 2002 07:07:00 -0400


It is dangerous to use Python to convert numbers from decimal to binary
and binary to decimal, if your instructor's point is for you to
understand the process. As a high school math teacher, I run into things
like this a lot, where students don't believe me that when they will be
tested on the homework, they won't be able to use their graphing
calculator, and then they don't bother to learn how to identify vertical
asymptotes and so can't recognize rational functions.

That said, here's a way to think about binary notation:

When we count, we /increment/ a number once each time, starting with
one. After we reach nine in the ones place, we don't have anywhere to
go, so we revert to zero in the ones place but increment the tens place.
One ten is equal to ten ones. This looks like this:

1
2
3
4
5
6
7
8
9
10  <--- we ran out of ones, and so threw them all out and added one to
the tens.

and then we keep counting, which I don't need to illustrate.

Binary is the same thing, except that instead of not having a "ten" that
fits in the ones place, we don't have a two. So it looks kind of like

  1
 10 <-- we ran out of ones
 11 <-- increment the ones
100 <-- we ran out of ones, and in incrementing the "twos", ran out
there, and so went out another digit.

And so on. Put your two columns of numbers next to each other in order
to translate small numbers. In order to translate large numbers,
recognize that just as 100==10**2 and 10==10**1 and 1==10**0 (each digit
of decimal is a power of ten) (** meaning exponentiation), so is each
digit of binary a power of two. 
Notice things about your chart. What is the ones digit in binary for
even numbers? How can you tell by looking at binary representation that
a number is one more than a multiple of four? What does it mean that
each digit of binary is a power of two, i.e., what does 256 look like?
1024?

So if you have to translate from binary to decimal, that's easy--

1001101 is 2**0 + 2**2 + 2**3 + 2**7 (reading from right to left)

going the other way is a tad bit more complicated; if someone gives you
141, you have to think, "hmm, the greatest power of two that is less
than or equal to 141 is 128, which is 2**7; so I should put a one in the
seventh place. Then 141-128==13. The greatest power of two that is less
than or equal to 13 is 8, which is 2**3, so I should put a one in the
third place. Then..." and keep going until you get to zero. 

Good luck.

(this part's not so much for Jimmy): Clearly this can be done in a loop,
where one first checks for zero and then takes the log base two of the
number, perhaps holding the places that get a one in a list. To take log
base two, do math.log(x)/math.log(2).

James M Lang wrote:
> 
> Okay, I've got an assignment from school for the summer. Some of the problems state to express the problem in binary notation. So basically, my question would be, could Python help me with my homework? Oh, and ah.., hehe, I forgot what binary notation is. Anyone know what it is?
> 
> _________________________________________
> Communicate with others using Lycos Mail for FREE!
> http://mail.lycos.com/
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor