Jump to content
IGNORED

Routines for binary and hex in basic


jchase1970
 Share

Recommended Posts

I needed binary operations in basic and I created some simple nibble operations routines to go from

dec to 4 bits

4 bits to dec

dec to hex

hex to dec

4 bits to hex

hex to 4 bits

 

I reserve variables DEC,HEX$,BITS$

They are string functions but if it is a decimal out come it will be a value not a string

 

All functions are a single line and could be set up as User-Defined Functions in extended basic

I have made them all gosubs so I can use them in compiled basic.

 

The code include a example of each function converting 0-15, 0-F, 0000-1111

Not sure if anyone needs these but I have used the binary functions many times including for bitwise operators which I will post next time.

 

I have made good use of them so maybe someone else can too :)

 

 

 

REM SET UP HEX AND BIN STRINGS
1 HEXSET$="0123456789ABCDEF"
2 BINSET$="B0000B0001B0010B0011B0100B0101B0110B0111B1000B1001B1010B1011B1100B1101B1110B1111"
REM RESERVE VARIABLEs
REM DEC,HEX$,BITS$

REM EXAMPLES
10 CALL CLEAR
20 PRINT"WORKING WITH A NIBBLE(4BITS)"
30 PRINT"SEND 0-15 AND RETURNS HEX$ 0-F"
40 FOR DEC=0 TO 15
50 GOSUB 1000
60 PRINT DEC,HEX$
70 NEXT DEC
80 GOSUB 900
90 PRINT"SEND HEX$ 0-F AND RETURN 0-15"
100 FOR VALUE=0 TO 15
110 DEC=VALUE
120 GOSUB 1000
130 DEC=0::REM CLEAR DEC VALUE TO KNOW WE ARE RETURNING A VALYE 
140 GOSUB 1010
150 PRINT HEX$,DEC
160 NEXT VALUE
170 GOSUB 900
180 PRINT"SEND 0-15 RETURN NIBBLE BIT VALUE"
190 FOR DEC=0 TO 15
200 GOSUB 1020
210 PRINT DEC,BITS$
220 NEXT DEC
230 GOSUB 900 
240 PRINT"SEND NIBBLE BITS$ AND RETURN DEC"
250 FOR VALUE=0 TO 15
260 DEC=VALUE
270 GOSUB 1020::REM GET BITS$ TO SEND
280 DEC=0 ::REM CLEAR DEC TO KNOW WE ARE RETURNING A DEC FOR BITS$
290 GOSUB 1030
300 PRINT BITS$,DEC
310 NEXT VALUE
320 GOSUB 900
330 PRINT"SEND HEX$ AND GET NIBBLE BITS$"
340 FOR DEC=0 TO 15
350 GOSUB 1000
360 GOSUB 1040
370 PRINT HEX$,BITS$
380 NEXT DEC
385 GOSUB 900
390 PRINT"SEND NIBBLE BITS$ AND RETURN HEX$ (0-F)"
400 FOR DEC=0 TO 15
410 GOSUB 1020::REM GET BIT FOR DEC
420 GOSUB 1050::REM GET HEX$ FOR BITS$
430 PRINT BITS$,HEX$
440 NEXT DEC
450 GOSUB 900
899 END
900 PRINT"PRESS ANY KEY"
910 CALL KEY(0,K,S)
920 IF S=0 THEN 910
930 RETURN



REM ALL ROUTINES FOR BIT FUNCTION FOR HEX AND BINARY NUMBERS
REM ALL STORED IN STRINGS FOR SAVING SPACE
REM ROUTINES WORK ON NIBBLES(4 BITS)
REM NO ERROR CHECKING

REM RETURN NIBBLE HEX FROM DEC
REM SET DEC TO 0-15 NUMBER
REM RETURN WILL BE HEX$
1000 HEX$=SEG$(HEXSET$,DEC+1,1)
1001 RETURN

REM RETURN NIBBLE DEC FROM HEX
REM SET HEX$ TO HEX VALUE
REM RETURN 0-15 IN DEC
1010 DEC=POS(HEXSET$,HEX$,1)-1
1011 RETURN

REM RETURN NIBBLE BITS FROM DEC
REM SET DEC TO 0-15
REM RETURNS NIBBLE BITS(4 BITS)
1020 BITS$=SEG$(BINSET$,DEC*5+2,4)
1021 RETURN

REM RETURN DEC FROM NIBBLE BITS
REM SEND BITS$ 
REM RETURNS DEC
1030 DEC=(POS(BINSET$,BITS$,1)-2)/5
1031 RETURN

REM RETURN NIBBLE BITS FROM HEX
REM SEND HEX VALUE IN HEX$
REM RETURNS BITS$
1040 BITS$=SEG$(BINSET$,(POS(HEXSET$,HEX$,1)-1)*5+2,4)
1041 RETURN

REM RETURN SINGLE HEX FROM NIBBLE BITS(4 BITS)
REM RETURNS 0-F FOR A 4 BIT STRING
REM SET BITS$ TO 4 BITS
1050 HEX$=SEG$(HEXSET$,(POS(BINSET$,BITS$,1)-2)/5+1,1)
1051 RETURN

 

 

Edited by jchase1970
  • Like 4
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...