11.07.2015 Views

Untitled - TRS-80 Color Computer Archive

Untitled - TRS-80 Color Computer Archive

Untitled - TRS-80 Color Computer Archive

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

INSIGHT:AtariBillWilkinsonNumber-BaseConversionsThis column was prompted by aletter in COMPUTEI's letters column, in which the author asked fora program to convert decimal numbers to binary. "Why," I asked myself, "do all these conversionprograms work with only one pairof bases (for example, base 10 tobase 2)?" Answer: because few realize that a more general program isalmost as easy these specific ones.Don't believe me? Keep reading.Number BasesYou probably learned about number bases back in third or fourthgrade, though you might not haverealized that's what you were learning. Specifically, you likely weretaught that the number 735 represented "seven hundreds, three tens,and five ones." The fact that digits ina number represent powers of ten iskind of an accident. If humans werenormally born with only three fingers and a thumb on each hand, youcan bet that 735 would have meant"seven sixty-fours, three eights, andfive ones" (that is, we would haveused base 8).Since computers are "born"with only two "fingers," their natural tendency is to use base 2, alsoknown as binary numbers or notation. (A computer's "fingers" are itsmemory cells, but each cell can remember only off or on, equivalentin function to counting on two human fingers.) Yet you seldom see acomputer memory dump printed inbinary, simply because such aprintout would be gigantic! Binarynumbers take up a lot of room compared to equivalent decimal numbers. Instead, because of the neatway that powers of two can begrouped together, we tend to seecomputer memory represented ineither octal (base 8) or hexadecimal(base 16) notation.One thing you may have noticed is that a base's number is thesame as the number of countingsymbols needed to represent it.Thus base 10 uses 0-9. Base 8 usesonly 0-7. What about bases beyond10, such as base 16, the hexadecimal base most often used in microcomputer work? Doesn't it need 16counting symbols? Yes, indeed, andthe symbols most commonly usedare 0-9, followed by A-F. (Why notuse completely new symbols for thedigits beyond 9? Simple: Early computer printers had only 64 differentsymbols available, so uppercase letters were used.)Why Hex?Sidelight: Since we are working oncomputers that tend to work withbytes, and since a byte can have avalue from 0 to 255 (decimal), base256 notation would seem to be alogical choice. But now we can seewhy it is not used—humans wouldbe forced to learn 256 unique digitsymbols! Still, there are two "nybbles"in each byte, and a nybble canhave a value from 0 to 15 (decimal),so hexadecimal (base 16) notation isa very logical alternative.Now, when you see a hexadecimal number such as A88C, whatdoes it mean? Well, you can readthat as "A four-thousand-ninetysixes,8 two-hundred-fifty-sixes, 8sixteens, and C ones." In turnthough, A and C may be read indecimal as 10 and 12, respectively.Whew! Now how about base 19?Confused? Don't worry, help isat hand. Program 1 consists of ashort main program followed bytwo special-purpose subroutines.These routines are designed tomake it easy to allow entry anddisplay of any number using anybase or pair of bases. The first one(from line 9200 to line 9330) takes anumber in variable N and convertsit to a string in variable N$ usingthe number base given by the variable BASE. The second routine(lines 9400-9560) performs thereverse operation, converting astring in N$ (which is supposed tobe a number in BASE notation) andconverting it to N for use as a number anywhere in BASIC.Try it. Type in the main codeand the subroutines and try the various options. And use some bizarrenumber bases, such as 13 or 37 or53. In keeping with the tradition ofhexadecimal numbering, the digitsymbols used are 0-9 (same as decimal for the first ten symbols), followed by A-Z, and then a-z (goodenough for anything up to base 62!).So now I have one set of routines which take care of all conversions. And it's kind of fun. Youcould even make a game of it: Tryto make two English words "equal"by changing bases! For example,RIB base 35 equals some animal(which happens to enjoy ribs) insome other base. Can you find theanimal word and its base? Maybetricks like this could make a hardto-breakencryption scheme? (Thiscan really cause you to lose sleep!)Be Just A Bit WiserI couldn't quit with simple numberconversions, of course. One of thehandy features of most higher-levellanguages is (usually) the presenceof operators which do bitwise operations. I like such operators somuch I put them into the first of theadvanced Atari-compatible BASICswe did, way back in 1981. Unfortunately, Atari BASIC does not havebitwise operators. In Atari BASIC,operators such as AND and OR always perform logical comparisonsrather than bitwise comparisons.Though, in fairness, I should pointout that there are occasions whereAtari's logical operators are worthas much as or more than bitwiseoperators. Some authors haveagreed with me to the extent thatthey have written machineFebruary 1987 COMPUTEI 73

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!