handling binary numbers

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
avtovaz
Posts: 74
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

handling binary numbers

Post by avtovaz » Sat Apr 11, 2020 12:36 pm

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.

User avatar
jgharston
Posts: 4084
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: handling binary numbers

Post by jgharston » Sat Apr 11, 2020 6:02 pm

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 32-bit 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,2005-2020
>_

avtovaz
Posts: 74
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: handling binary numbers

Post by avtovaz » Sat Apr 11, 2020 8:30 pm

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!

User avatar
BeebMaster
Posts: 3540
Joined: Sun Aug 02, 2009 5:59 pm
Location: Lost in the BeebVault!
Contact:

Re: handling binary numbers

Post by BeebMaster » Sun Apr 12, 2020 12:40 pm

If you want to test individual bits (0-7) 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:

Code: Select all

   10B%=42
   20FOR C%=0 TO7
   30IF B% AND 2^C% PRINT"1"; ELSE PRINT"0";
   40NEXT
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:
Screenshot_2020-04-12_12-36-49.png
Image

User avatar
LordVaderUK
Posts: 208
Joined: Thu Jan 31, 2019 12:26 am
Location: Hampshire
Contact:

Re: handling binary numbers

Post by LordVaderUK » Mon Apr 13, 2020 1:46 pm

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.

Code: Select all

   10 REPEAT
   20   MODE 7
   30   D$=""
   40
   50   REPEAT:INPUT "Enter decimal number (0-255):";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? (0-7):";
  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 STEP-1
  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 (255-2^B1%) ELSE R%=A1% OR 2^B1%
  480 =R%
  490
  500 DEFFNSETBIT(A1%,B1%,V%)
  510 IF V%=0 R%=A1% AND (255-2^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 17 times
Loving my BBC Master 128

User avatar
Richard Russell
Posts: 1663
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: handling binary numbers

Post by Richard Russell » Mon Apr 13, 2020 6:06 pm

jgharston wrote:
Sat Apr 11, 2020 6:02 pm
The Number library at mdfs.net/blib has FNb() and FNb0() which display 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, :

Code: Select all

      INSTALL @lib$ + "stringlib"
      PRINT FN_binary(42)
      PRINT FN_tobase(42,2,8)
which produces:

Code: Select all

101010
00101010

Post Reply

Return to “programming”