RosettaCodeData/Task/Bitwise-IO/BBC-BASIC/bitwise-io.basic

44 lines
1.1 KiB
Plaintext

file$ = @tmp$ + "bitwise.tmp"
test$ = "Hello, world!"
REM Write to file, 7 bits per character:
file% = OPENOUT(file$)
FOR i% = 1 TO LEN(test$)
PROCwritebits(file%, ASCMID$(test$,i%), 7)
NEXT
PROCwritebits(file%, 0, 0)
CLOSE #file%
REM Read from file, 7 bits per character:
file% = OPENIN(file$)
REPEAT
ch% = FNreadbits(file%, 7)
VDU ch%
UNTIL ch% = 0
PRINT
CLOSE #file%
END
REM Write n% bits from b% to file f% (n% = 0 to flush):
DEF PROCwritebits(f%, b%, n%)
PRIVATE a%, c%
IF n% = 0 BPUT #f%,a% : a% = 0 : c% = 0
WHILE n%
IF c% = 8 BPUT #f%,a% : a% = 0 : c% = 0
n% -= 1
c% += 1
IF b% AND 1 << n% THEN a% OR= 1 << (8 - c%)
ENDWHILE
ENDPROC
REM Read n% bits from file f%:
DEF FNreadbits(f%, n%)
PRIVATE a%, c% : LOCAL v%
WHILE n%
IF c% = 0 a% = BGET#f% : c% = 8
n% -= 1
c% -= 1
v% = v% << 1 OR (a% >> c%) AND 1
ENDWHILE
= v%