Basic Programming with DEF FN() and Boolean Logic

Authors

Publication

Pub Details

Date

Pages

See all articles from Update July 1991

Don’t go away just yet! The program given with this article is a useful one. And it will work with whatever kind of computer you may have. So, lets see just what the program does first. Then you can decide whether or not to key it in and whether you’d like to explore the two subjects of discussion- Boolean Logic and the DEF FN() function.

NUMBER CONVERSIONS: The program takes any input of Decimal, Hex or Binary numbers and converts and prints all three equilivants.

MIXED MATH: Can you Multiply 110 hex times F hex? Can you Divide 104926 by FF hex? Can you subtract “FF02h – BIN 11110110”? Well I cannot do that. But this program eats it up. You can ADD mixed numbers, subtract mixed numbers, divide with mixed numbers, and multiply mixed numbers. The answers are given in all three number bases. But, when doing divisions, and the answer is a fraction, the HEX and Binary answers will be wrong. But not to worry, the given Decimal answer will be correct. Also, when doing mixed math, you can input the Decimal values as math expressions, ie: 2PIr, L*W, if such variables have value. You could even input as a decimal value “FN a()”, making the computer solve a FN function for the input. For BIN inputs be sure to use the BIN token before the number.

BOOLEAN LOGIC is a natural for all computers. Boolean logic expressed in Basic Language allows us to “package” groups of both literal and math expressions in one line of programming. The capacity of the screen (for entering the program line) is the only limitation. Several program lines in the demo program would require up to 20 other program lines if the logicals were expressed in common Basic. And, not only does the program work faster with Boolean logic, but the amount of memory used for the program is reduced substantially. There are some good reference articles about Boolean in back issues of Update magazine that can serve as a primer on the subject. (July 1988 Pg 13), (April 1988 pg 18).

Almost all computers provide some ability to use BOOLEAN expressions in Basic Language. Some computer’s “syntax checking” are very liberal while others may trap out some perfectly valid Boolean expressions. One way of forcing the computer to process Boolean expressions is to Disable the computer’s syntax checker. Almost all computers provide an address to be POKED to disable the syntax checking function. So, if your computer is one that disallows some variations of Boolean expressions then it might be worth while to explore explore the method of “forcing your will on the cantankerous beast” by simply by-passing the syntax checker for your lines of programming that contain valid boolean logic.

The Demo program does work. However, the TS-2068 is more liberal in allowing Boolean logic expressions than most other computers. So if yours balks at any of the programming it is a cantankerous charactistic of your computer’s syntax checker and it would be worth while to find a by-pass.

DEF FN() and FN Functions

Several of my articles on the subject of DEF FN() have been leading up to this program demonstration. Particularily, the Jan. 1991 and April 1991 issues of of Update are appropriate for background. This time the programming demonstrates the versatility of DEF FN() functions expressed in Boolean logic for both literal function definition and math definitions. Line #9700 concatenates Y$ to encapsulate the HEX value expression depending upon six possible situations. So, the upper limit of both Number conversions and mixed number calculations areset at FFFFFF hex, or 16777215 decimal (16 megabytes). The limit is set by LEN FN x$(). This DEF FN x$() function in line #9700 should be proof enough of the vast powers of the DEF FN () function.

And, isn’t it interesting that the DEF FN x$() can be concatenated just as if we were dealing with a string variable. Also, individual characters of the DEF FN x$() can be picked out in the same manner as characters in a simple string. But DEF FN x$() is NOT a variable. We can have “X$” and DEF FN X$() as separate and non associated entities. Then there are six numeric DEF FN() functions beginning at line #9710, all using Boolean logic. In these six cases the math formula (function) use FN to call up the solution of the DEF FN x$() function of line #9700. The main program then is responsible for setting the correct value of Y$ before the seven DEF FN functions are exercised.

Although the DEF FN() functions demonstrated are phenomonal, all were once in program lines of Boolean logic that dealt with the dimensioned X$. And the program worked faster that way. The logic was pulled out of program lines and installed in DEF FN functions just for Demo purposes. In some cases simple programming is better and in others the DEF FN function works better or exclusive for the purpose.

The use of DEF FN also avoids GO SUB branching. Instead one just uses FN to get the work that a sub-routine work branch would otherwise provide. Complex calculations can be “in trains” of DEF functions where each succeeding DEF FN function requires the solution of the earlier functions. When FN is used, the protocol is “first line first” for the finding and solution of the DEF FN functions. I hope that these three articles (beginning with the Jan. 91 Update issue) will provide the enthusiasm for all to explore the power of both Boolean programing and the use of the DEF FN() function. Have fun!

Products

 

Media

 

Image Gallery

Source Code

 9700 DEF FN x$() = ("00000"+y$ AND LEN y$=1 ) + ("0000"+y$ AND LEN y$=2)+("000"+y$ AND LEN y$=3)+("00"+y$ AND LEN y$=4)+("O"+y$ AND LEN y$=5)+(y$ AND LEN y$=6) 
 9702 REM 
 9710 DEF FN a()= (1048576*(CODE FN x$() (1) -0) AND CODE FN x$()(1)<r)+(1048576* (CODE FN x$() (1) -P) AND CODE FN x$() (1)>g) 
 9712 REM 
 9720 DEF FN b())= (65536*(CODE FN x$()(2)-o) AND CODE FN x$() (2)<r)+ (65536*(CODE FN x$() (2)-P) AND CODE FN x$()(2)>g) 
 9722 REM 
 9730 DEF FN c() = (4096* (CODE FN x$() (3)-o) AND CODE FN x$()(3)<r)+(4096*(CODE FN x$ () (3) -P) AND CODE FN x$ () (3)>g) 
 9732 REM 
 9740 DEF FN d() = (256* (CODE EN x$() (4)-o) AND CODE FN x$() (4)<r)+(256* (CODE FN x$() (4)-P) AND CODE FN x$() (4)>g) 
 9742 REM 
 9750 DEF FN e()= (16*(CODE FN x$()(5)-o) A ND CODE EN x$() (5) <r) +(16*(CODE FN x$()(5 )-P) AND CODE FN x$() (5)>g) 
 9752 REM 
 9760 DEF FN f() = (CODE FN x$() (6)-o AND CO DE FN x$()(6)<r)+ (CODE FN x$() (6)-P AND C ODE FN x$() (6)>g)
 9770 REM 
 9780 BORDER 1: PAPER 1: INK 7: CLS : POKE 23658,8 
 9800 CLS : LET xy=0: LET T=O: LET o=48: L ET p=55: LET g=64: LET r=65: LET a=1: DIM u(24): FOR n=1 TO 24: LET u(n)=a: LET a= ata: NEXT n 
 9804 CLS : PRINT AT 9,0; "Select: 1. Convert Hex-Dec-BIN 2. Mixed Math": PAUSE O: LET M=CODE INKEY$-48: CLS : GO TO (m<1 OR m>2)*9804+(m=1)*9812+(m=2)*9886 
 9812 DIM x$(6): PRINT AT 9,0; "1. Dec to Hex, or 2. Hex to Dec 3. Quit?": PAUSE O: LET X=CODE INKEY$-48: IF X=1 THEN GO TO VAL "9822" 
 9814 IF x=3 THEN GO TO VAL "9804" 
 9816 DIM x$(6): INPUT "HEX Group:"; y$: IF LEN y$>6 THEN GO TO 9816 
 9818 LET DEC=FN a() +FN b() +EN c() +FN d() + FN e() +FN f() : LET x$=FN x$(): LET M$=FN x$(): IF xy=1 THEN RETURN
 9820 GO TO 9826 
 9822 INPUT "Input Decimal OR BIN Number: "; DEC 
 9824 DIM x$(6): LET A=DEC: LET B1=INT (A/1048576): LET A=A-B1*1048576: LET x$(1)=( CHR$ (B1+O) AND B1<10) + (CHR$ (B1+P) AND B 1>9): LET b2=INT (A/65536): LET A=A-B2*65 536: LET x$(2)= (CHR$ (B2+0) AND B2<10)+(C HR$ (B2+P) AND B2>9): LET B3=INT (A/4096) : LET A=A-B3*4096: LET x$(3) = (CHR$ (B3+0) AND B3<10) +(CHR$ (B3+P) AND B3>9): LET B 4= INT (A/256): LET A=A-B4*256: LET x$(4)= (CHR$ (B4+O) AND B4<10) + (CHR$ (B4+P) AND B4>9): LET B5= INT (A/16): LET A=A-B5*16: LET x$(5)= (CHR$ (B5+0) AND B5<10)+(CHR$ ( B5+P) AND B5>9): LET x$(6)= (CHR$ (A+O) AN D A<10) + (CHR$ (A+P) AND A>9): LET M$=x$: IF xy=1 THEN RETURN 
 9826 CLS : PRINT "DEC="; DEC; TAB 15; "HEX=" ; 
 9828 FOR n=1 TO 6: IF m$ (n)>"0" THEN FOR n=n TO 6: PRINT m$ (n) ;: NEXT n: GO TO 98 32 
 9830 NEXT n 
 9832 GO SUB 9946: GO TO 9812
 9886 LET xy=1: PRINT AT 9,0; "Select Choice: <1> Add <2> Subtract <3> Multiply <4> Divide <5> Home Menu ": PAUSE O: LET g1=CODE INKEY$-48: IF gl<1 OR g1>5 THEN GO TO 9886
 9888 IF gl=5 THEN GO TO 9804 
 9890 CLS : PRINT #0; "First Number HEX or DEC/BIN? <1> Hex <2> Dec/Bin": PAUSE O: LET M1=CODE INKEY$-48:: IF ml<1 OR ml >2 THEN GO TO 9890 
 9892 CLS : PRINT #0; "Second Number HEX OI Dec/Bin? <1> HEX <2> Dec/Bin ": PAUSE O: LET m2=CODE INKEY$-48: IF m2<1 OR m2> 2 THEN GO TO 9892 
 9894 IF m1=1 THEN INPUT "INPUT First Number (HEX) "; y$: LET H$=Y$: IF y$="" THEN GO TO 9894 
 9896 IF ml=2 THEN INPUT "INPUT First Number (Dec/Bin) ";y$: LET H$-Y$: LET n1= VAL y$: IF y$=**"' THEN GO TO 9896 
 9898 IF m2=1 THEN INPUT "INPUT Second Number (HEX): ";z$: LET I$=Z$: IF z$="" THE N GO TO 9898 
 9900 IF m2=2 THEN INPUT "INPUT Second Number (Dec/Bin) : ";z$: LET I$=Z$: LET n2= VAL z$: IF Z$="" THEN GO TO 9900
 9902 DIM x$(6): IF ml=1 THEN GO SUB 9818 : LET n1=dec: LET x$= (y$ AND LEN y$=6)+(" O"+y$ AND LEN y$=5)+("00"+y$ AND LEN y$=4 )+ ("000"+y$ AND LEN y$=3)+("0000"+y$ AND LEN y$=2)+("00000"+y$ AND LEN y$=1)+("000000" AND y$="*"): GO SUB 9818: LET n1=DEC 
 9904 IF M2=1 THEN LET y$=z$: GO SUB VAL "9818": LET N2=DEC 
 9906 IF G1=1 THEN LET DEC=N1+N2: GO VAL "9824": PRINT H$; ("h" AND M1=1)+ ("a" AND M1=2);"+"; z$; ("'h" AND M2=1)+ ("'a" AND M2=2);"="; DEC; "a";" and "; 
 9908 IF G1=1 THEN FOR N=1 TO 6: IF M$ (N) >"O" THEN FOR N=N TO 6: PRINT M$(N) ;: NE XT N: PRINT "h"': GO TO 9912 
 9910 IF G1=1 THEN NEXT N: PRINT "Oh" 
 9912 IF G1=2 THEN LET DEC=N1-N2: GO SUB 9824: PRINT H$; ("h" AND MI=1) + ("a" AND MI =2);"-"; z$; ("'h" AND M2=1)+("d" AND M2=2) ; "="; DEC; "d and ";
 9914 IF G1=2 THEN FOR N=1 TO 6: IF x$(N) >"O" THEN FOR N=N TO 6: PRINT x$(N);: NE XT N: PRINT "h"': GO TO 9918 9916 IF G1=2 THEN NEXT N: PRINT "Oh"''
 9918 IF G1=3 THEN LET DEC=N1*N2: GO SUB 9824: PRINT H$; ("'h" AND M1=1) + ("a" AND MI =2);" * ";z$; ("h" AND M2=1) + ("d" AND M2=2 ) ; " =";DEC; "a";" and ";
 9920 IF G1=3 THEN FOR N=1 TO 6: IF x$(N) >"0" THEN FOR N=N TO 6: PRINT x$(N) ;: NE XT N: PRINT "h"': GO TO 9924 
 9922 IF G1=3 THEN NEXT N: PRINT "Oh"' 9924 IF G1=4 THEN LET DEC=N1/N2: GO SUB 9824: PRINT H$; ("h" AND M1=1) + ("a" AND MI =2);" / "'; z$; ("h" AND M2=1) + ("d" AND M2=2 ); "="; DEC; "d and "; 
 9926 IF G1=4 THEN FOR N=1 TO 6: IF x$(n) >"O" THEN FOR N=N TO 6: PRINT x$(N);: NEXT N: PRINT "h"': GO TO 9930 
 9928 IF G1=4 THEN NEXT N: PRINT "Oh"' 
 9930 GO SUB 9946: GO TO 9886 
 9932 GO TO VAL "9886" 
 9946 LET m-dec: DIM t(24): FOR n=24 TO 1 STEP -1: LET t(n)=(1 AND m›=u(n)) + (0 AND mu(n)): IF t(n)=1 THEN LET m=m-u(n) 
 9948 NEXT n 
 9950 LET t$="": FOR n=24 TO 1 STEP -1: IF t (n)=1 THEN FOR y=1 TO n: LET t$=t$+STR $ t(y): NEXT y: GO TO 9953 
 9952 NEXT n 
 9953 LET m$="": FOR n=LEN t$ TO 1 STEP -1 : LET m$=m$+T$(n): NEXT n 
 9954 PRINT ''"BIN=";m$: RETURN
Scroll to Top