Has anyone got an example of a program handling 8 bit numbers that i can have a look at. Im a bit of a novice at basic and im using a Pi with Richard Russels BBC basic to control a 8 bit register chip with the gpios. Im not 100% sure what to do or what i can do with bbc basic in this way.
thanks.
handling binary numbers
Re: handling binary numbers
The Number library at mdfs.net/blib has FNb() and FNb0() which display binary numbers:
290 REM Binary padded with zeros
300 DEFFNb0(A%,N%):LOCAL A$,B$,L%:B$="0":IFA%<0:B$="1":A%=A%AND&7FFFFFFF
310 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:L%=L%+1:UNTILL%>30:=RIGHT$(B$+A$,N%)
320 :
330 REM Binary padded with spaces
340 DEFFNb(A%,N%):LOCAL A$:IFA%<0:=FNb0(A%,N%)
350 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:UNTILA%=0:=RIGHT$(STRING$(N%," ")+A$,N%)
eg: PRINT FNb0(42,6) gives 101010.
Using 32bit BASIC (eg Windows, RISC OS, Linux, Pi, etc.) binary constants are prefixed with %, so PRINT %101010 gives 42.
290 REM Binary padded with zeros
300 DEFFNb0(A%,N%):LOCAL A$,B$,L%:B$="0":IFA%<0:B$="1":A%=A%AND&7FFFFFFF
310 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:L%=L%+1:UNTILL%>30:=RIGHT$(B$+A$,N%)
320 :
330 REM Binary padded with spaces
340 DEFFNb(A%,N%):LOCAL A$:IFA%<0:=FNb0(A%,N%)
350 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:UNTILA%=0:=RIGHT$(STRING$(N%," ")+A$,N%)
eg: PRINT FNb0(42,6) gives 101010.
Using 32bit BASIC (eg Windows, RISC OS, Linux, Pi, etc.) binary constants are prefixed with %, so PRINT %101010 gives 42.
Code: Select all
$ bbcbasic
PDP11 BBC BASIC IV Version 0.32
(C) Copyright J.G.Harston 1989,20052020
>_
Re: handling binary numbers
thankyou!! that is ideal, ill have to work out what it all means and how to use it, but its easier for me to understand that way..
thankyou again!
thankyou again!
 BeebMaster
 Posts: 3328
 Joined: Sun Aug 02, 2009 5:59 pm
 Location: Lost in the BeebVault!
 Contact:
Re: handling binary numbers
If you want to test individual bits (07) in a byte you can use AND with the value of that bit within the byte.
42 AND 16 will give you 0, because bit 4 is zero
42 AND 32 will give you 32, because bit 5 is 1.
To better understand which value relates to which bit you use powers of 2, so 2^(bit number) will do the same test:
42 AND 2^5 gives 0
42 AND 2^6 gives 1
Or loop through all 8 bits in a byte:
Or loop up to 15, 31 or 63 etc. for a 2, 3 or 4 byte etc. number.
Ubuntu calculator can do this sort of thing as well, probably there are others:
42 AND 16 will give you 0, because bit 4 is zero
42 AND 32 will give you 32, because bit 5 is 1.
To better understand which value relates to which bit you use powers of 2, so 2^(bit number) will do the same test:
42 AND 2^5 gives 0
42 AND 2^6 gives 1
Or loop through all 8 bits in a byte:
Code: Select all
10B%=42
20FOR C%=0 TO7
30IF B% AND 2^C% PRINT"1"; ELSE PRINT"0";
40NEXT
Ubuntu calculator can do this sort of thing as well, probably there are others:
 LordVaderUK
 Posts: 202
 Joined: Thu Jan 31, 2019 12:26 am
 Location: Hampshire
 Contact:
Re: handling binary numbers
Funny enough, I was working on this yesterday, trying to get my head around it. I want to use a single byte to store various flags for use in a text adventure game. I found a good description in a book, How to write Adventure Games by Peter Killworth  see attached.
I then wrote a little program which allows you to enter a number, and then check, set or toggle bits. Apologies for any clunkiness in the code but you may find it useful.
I then wrote a little program which allows you to enter a number, and then check, set or toggle bits. Apologies for any clunkiness in the code but you may find it useful.
Code: Select all
10 REPEAT
20 MODE 7
30 D$=""
40
50 REPEAT:INPUT "Enter decimal number (0255):";A%:UNTIL A%>=0 AND A%<=255
60
70 PROCDISPNUM(A%)
80
90 REPEAT
100 PRINT "(C)heck or (T)oggle or (S)et?":REPEAT:A$=GET$:UNTIL INSTR("CcTtSs",A$)
110
120 IF A$="C" OR A$="c" PRINT "Check"; : O%=0
130 IF A$="T" OR A$="t" PRINT "Toggle";: O%=1
140 IF A$="S" OR A$="s" PRINT "Set"; : O%=2
150 PRINT " which bit? (07):";
160
170 REPEAT:INPUT B%:UNTIL B%>=0 AND B%<=8
180
190 IF O%=2 PRINT "Set to 0 or 1?":REPEAT:INPUT V%:UNTIL V%=0 OR V%=1
200
210 IF B%<>8 AND O%=0 PROCCHECKBIT(A%,B%)
220
230 IF B%<>8 AND O%=1 A%=FNTOGGLEBIT(A%,B%): PROCDISPNUM(A%)
240
250 IF B%<>8 AND O%=2 A%=FNSETBIT(A%,B%,V%): PROCDISPNUM(A%)
260
270 UNTIL B%=8
280 UNTIL FALSE
290
300 DEFPROCCHECKBIT(A1%,B1%)
310 REM PRINT A1%,B1%
320 IF FNCHECKBIT(A1%,B1%) PRINT "Set" ELSE PRINT "Not Set"
330 ENDPROC
340
350 DEFFNBINARY(E%)
360 D$=""
370 FOR D%=7TO0 STEP1
380 N% = 2^D%
390 IF E% AND N% D$=D$+"1": ELSE D$=D$+"0"
400 NEXT
410 =D$
420
430 DEFFNCHECKBIT(A1%,B1%)
440 =A1% AND 2^B1%
450
460 DEFFNTOGGLEBIT(A1%,B1%)
470 IF FNCHECKBIT(A1%,B1%) R%=A1% AND (2552^B1%) ELSE R%=A1% OR 2^B1%
480 =R%
490
500 DEFFNSETBIT(A1%,B1%,V%)
510 IF V%=0 R%=A1% AND (2552^B1%) ELSE R%=A1% OR 2^B1%
520 =R%
530
540 DEFPROCDISPNUM(A%)
550 PRINT "76543210"';FNBINARY(A%);" (";STR$(A%);")"
560 ENDPROC
 Attachments

 Binary.pdf
 (1019.6 KiB) Downloaded 14 times
Loving my BBC Master 128
 Richard Russell
 Posts: 1441
 Joined: Sun Feb 27, 2011 10:35 am
 Location: Downham Market, Norfolk
 Contact:
Re: handling binary numbers
The stringlib.bbc library (supplied with BBC BASIC for Windows and BBC BASIC for SDL 2.0) includes the FN_binary() and FN_tobase() functions which can be used to convert to binary, :jgharston wrote: ↑Sat Apr 11, 2020 6:02 pmThe Number library at mdfs.net/blib has FNb() and FNb0() which display binary numbers:
Code: Select all
INSTALL @lib$ + "stringlib"
PRINT FN_binary(42)
PRINT FN_tobase(42,2,8)
Code: Select all
101010
00101010