ORIGINALLY PUBLISHED IN LIMA NEWSLETTER SEPTEMBER 1990 ^^^^^^^^^^^^^^ NO MYSTERY ABOUT 32767 ^^^^^^^^^^^^^^^^^by Alexander Hulpke (BB&&P editor's note: This article is a response to "The Mystery of 32767" by Andy Frueh, published in the June 1990 issue of BB&&P. Alexander is the author of a TI version of Tetris, XHi, YAPP, and other fairware software.) If you had to remember a phone number, which would be easier: 123-456-7890 or 314-159-2653 ? Surely the first one ! If you had to describe a two dimensional object, which would be easier: A circle or a oddly shaped polygon? Most likely the first. What is the reason for these examples: They show, that "simplicity" is very often a question of your point of view: If you are regarding numbers, as a ten-fingered person using the decimal system, the different symbols in their natural order are quite simple. If you regard objects, you look at their geometrical properties. If you were to regard them as numbers, for example calculating the area, the "simplicity" would be quite different: A polygon will most likely have an rational area, for example 523/45. The area of the unit circle cannot be described rational, it is an irrational number, its decimal evaluation starting with 3.141592653 . You have seen, that "simplicity" is not always the same, it depends on what you think to be the important part of am object. Let's think, you were a computer. As we all know, computers can only count 1 and 0. Which numbers would be more simple: 1000 0000 0000 0000 or 1100 0011 0101 0000?^^Most likely the first one. Since we are decimal creatures, we should want to regard these numbers as decimals: The first one is 32768, the second one is 50000, much more "simple" than 32768. You see, that the powers of two: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 and 65536 are "simple" numbers for dual creatures like computers, which also means that building something using these numbers is easy. For example, it means: Line 0 is Hi Level, the other lines are ALL Lo Level. This is the reason for the proliferation of these powers in computers: We have a 16 Bit processor, having 64k=65536 Bytes Address Space (1k is 1024 Byte), the screen is 32 characters wide, each an 8*8 grid, thus having 256 horizontal resolution (the lower vertical resolution is due to the compatibility with the TV system, which is not a digital invention). We have 256 characters (though Basic allows only 128, thus saving some memory), that may fill a string up to size 255 (+1 size byte is 256). etc. etc. But why 32767 which is clearly 2 to the power of 15 -1 ! Why the -1 ??? We all know, that there are two kinds of numbers: positive and negative. If we have to count apples, surely no negative counts can be possible. Thus, if we have numbers 1 to 65536, we could use them. Now instead of counting apples, lets look at the thermometer (having only an integer scale). It might be necessary to use also negative numbers. If you have only positive numbers, what do you do?^^You would surely take (perhaps half of them) and CALL them negative numbers. So you would get 32768 positive and 32768 negative numbers. But wait: There also is a a number, neither positive, nor negative; the 0. We have to spare one number to be able to display a 0. Shell we take a positive or a negative one?^^To understand what is done, lets look closely at calculating with a finite set of numbers: If its 22 o'clock and you "add" 4 hours, its 2. We were using a system of 0 to 23 and were "throwing the 24 away", regarding it again as zero. Thus 26=24+2 becomes 0+2=2. What we have done is usually called "modulo arithmetic". A funny result is, that also negative numbers can be positive. Modulo 24 you can say either 22 or -2 since 2+22=0. This arithmetic has very interesting properties. For example, you can show that a full calculations; which means, that you may add, subtract, multiply AND divide each two numbers and get a result again IN THE SAME SET, you started in (The mathematicians call a set, that has these properties "field") if and only if the number, by which you were doing the modulo arithmetic is a prime number. If you only add, subtract and multiply, it can be any number (this is called "ring"). Further examinations of these numbers lead far into the subject of abstract algebra, and we shall not cover this subject in this article much further. Coming to the computer again, we already know that the powers of two are very important. Especially, since we have a 16 Bit processor, we have 16 Bit registers, which means, we have 65536 possible numbers for each register. So we will do again modulo arithmetic (note that 65536 is NOT prime, thus we cannot do fully division, but most times get a remainder, so the DIV command in machine language will always give quotient and remainder): As we have seen, -10 will be the "same" as 65526. There comes a very neat idea: if you add 65536 (which is in fact the 0 in our modulo arithmetic) to a number, you will get a positive number, that is the "same". We will cover this later. This fact leads us to the system of number representations, used in the 9900. It is called "two's complement" and works like the following: To create a negative number, just invert all bits, and add 1 to the result. Thus -32767 is: 32767 equals 0111 1111 1111 1111 inverted: 1000 0000 0000 0000, add 1: 1000 0000 0000 0001 equals 32769, 32767+32769 = 65536 equals 0. Voila! The interesting property is: Since its modulo arithmetic, its fully compatible with larger numbers. You can use either positive numbers 0 to 65535 or numbers -32768 to 32767 with the SAME routines for calculation. Its only dependent on your point of view! If you regard numbers as negative, you can easily say if a number is negative, just by looking at it's first bit, that contains the sign. Of course, its not as simple as using Bits 1 to 15 for the number and Bit 0 for the sign, but then you would need different calculating procedures. Now we can understand the multiple occurences of 32767: Whenever Basic passes a number to Assembler, the number is regardes as to be signed. When we address memory, as for LOAD and PEEK, we calculate only with positive numbers. So we must subtract the number from 65536 to get the correct negative number if the address is too big to be a "register positive". Other routines, that do not need to get numbers that big, would be very confusing: What is a negative position in a string? Thus SEG$ and POS are limited to 32767. Negative line numbers are not allowed, thus the largest line number is 32767^^By the way: Line number 0 may not be entered, but you can create it by some manipulations. A program with line 0 will run correctly. The fact that RES will place 32767 for each line number that could not be found is quite arbitrary. If you look at address >2314 in the Basic Interpreter (see TI Intern, pg.137), you will detect, that >7FFF equals 32767 is taken as an error value. I guess this was done, since most time this line will not exist and the program will break with an error instead of running into other routines that were prohibited. In fact only the line number 32767 generated by RES is arbitrary, the other occurences of 32767 in the TI are due to the internal number format. .PL 1