Bitwise IO: Difference between revisions

Replace deprecated functions
m (→‎{{header|Wren}}: Changed to Wren S/H)
(Replace deprecated functions)
 
(4 intermediate revisions by 3 users not shown)
Line 26:
 
<syntaxhighlight lang="11l">T BitWriter
FileFileWr out
accumulator = 0
bcount = 0
 
F (fname)
.out = File(fname, ‘w’WRITE)
 
F _writebit(bit)
Line 61:
 
F (fname)
.input = File(fname, ‘r’)
 
F _readbit()
I .bcount == 0
V a = .input.read_bytes(at_most' 1)
I !a.empty
.accumulator = a[0]
Line 100:
12345abcde
</pre>
 
=={{header|6502 Assembly}}==
===Storing Bytes As ASCII Strings===
Line 665 ⟶ 666:
Hello, world!
</pre>
=={{header|Binary Lambda Calculus}}==
As explained on https://www.ioccc.org/2012/tromp/hint.html, BLC program
<pre>44 68 16 05 7e 01 17 00 be 55 ff f0 0d c1 8b b2 c1
b0 f8 7c 2d d8 05 9e 09 7f bf b1 48 39 ce 81 ce 80</pre>
compresses a string of ASCII "0"/"1" bytes into an 8x smaller stream of bytes, padding the final byte with 0 bits.
 
=={{header|C}}==
MSB in a byte is considered the "first" bit. Read and write methods somewhat mimic fread and fwrite, though there's no fflush-like function because flushing bits into a file is ill-defined (this whole task is pretty ill-defined). Only way to make sure all bits are written to the file is by detaching the bit filter, just like how closing a file flushes out buffer. There's no limit on read/write size, but caller should ensure the buffer is large enough.
Line 781 ⟶ 788:
return 0;
}</syntaxhighlight>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
Line 1,593 ⟶ 1,601:
dup 0= if 2drop init-read then
2dup and swap 2/ swap ;</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Type BitFilter
nombre As String
accu As Integer
bits As Integer
bw As Integer
br As Integer
offset As Integer
End Type
 
Sub openWriter(bf As BitFilter)
bf.bw = Freefile
Open bf.nombre For Binary Access Write As #bf.bw
End Sub
 
Sub openReader(bf As BitFilter)
bf.br = Freefile
Open bf.nombre For Binary Access Read As #bf.br
bf.offset = 0
End Sub
 
Sub escribe(bf As BitFilter, buf() As Byte, start As Integer, nBits As Integer, shift As Integer)
Dim As Integer index = start + (shift \ 8)
shift Mod= 8
While nBits <> 0 Or bf.bits >= 8
While bf.bits >= 8
bf.bits -= 8
Dim As Byte outByte = ((bf.accu Shr bf.bits) And &hFF)
Put #bf.bw, , outByte
Wend
While bf.bits < 8 And nBits <> 0
Dim As Byte b = buf(index)
bf.accu = (bf.accu Shl 1) Or (((128 Shr shift) And b) Shr (7 - shift))
nBits -= 1
bf.bits += 1
shift += 1
If shift = 8 Then
shift = 0
index += 1
End If
Wend
Wend
End Sub
 
Sub lee(bf As BitFilter, buf() As Byte, start As Integer, nBits As Integer, shift As Integer)
Dim As Integer index = start + (shift \ 8)
shift Mod= 8
While nBits <> 0
While bf.bits <> 0 And nBits <> 0
Dim As Byte mask = 128 Shr shift
buf(index) = Iif((bf.accu And (1 Shl (bf.bits - 1))) <> 0, (buf(index) Or mask) And &hFF, (buf(index) And Not mask) And &hFF)
nBits -= 1
bf.bits -= 1
shift += 1
If shift >= 8 Then
shift = 0
index += 1
End If
Wend
If nBits = 0 Then Exit Sub
Dim As Byte inByte
Get #bf.br, bf.offset + 1, inByte
bf.accu = (bf.accu Shl 8) Or inByte
bf.bits += 8
bf.offset += 1
Wend
End Sub
 
Sub closeWriter(bf As BitFilter)
If bf.bits <> 0 Then
bf.accu Shl= (8 - bf.bits)
Dim As Byte outByte = (bf.accu And &hFF)
Put #bf.bw, , outByte
End If
Close #bf.bw
bf.accu = 0
bf.bits = 0
End Sub
 
Sub closeReader(bf As BitFilter)
Close #bf.br
bf.accu = 0
bf.bits = 0
bf.offset = 0
End Sub
 
Dim As Integer i
Dim As BitFilter bf
bf.nombre = "test.bin"
 
' for each byte in s, write 7 bits skipping 1
Dim As Byte s(10) => {Asc("a"), Asc("b"), Asc("c"), Asc("d"), _
Asc("e"), Asc("f"), Asc("g"), Asc("h"), Asc("i"), Asc("j"), Asc("k")}
openWriter(bf)
For i = 0 To Ubound(s)
escribe(bf, s(), i, 7, 1)
Next i
closeWriter(bf)
 
' read 7 bits and expand to each byte of s2 skipping 1 bit
openReader(bf)
Dim As Byte s2(0 To Ubound(s)) = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
For i = 0 To Ubound(s2)
lee(bf, s2(), i, 7, 1)
Next i
closeReader(bf)
 
For i = 0 To Ubound(s2)
Print Chr(s2(i));
Next i
Print
 
Sleep</syntaxhighlight>
{{out}}
<pre>abcdefghijk</pre>
 
=={{header|Go}}==
Based on the
Line 3,360 ⟶ 3,486:
}</syntaxhighlight>
=={{header|Seed7}}==
The Seed7 library [httphttps://seed7.sourceforge.net/libraries/bitdata.htm bitdata.s7i] defines
several functions to do bitwise I/O. Bitwise data can be read from (or written to) a string or a file.
The direction of bits can be from LSB (least significant bit) to MSB (most significant bit) or vice versa.
In the program below the functions
[httphttps://seed7.sourceforge.net/libraries/bitdata.htm#putBitsMsb(inout_file,inout_integer,in_var_integer,in_var_integer) putBitsMsb], [https://seed7.sourceforge.net/libraries/bitdata.htm#openMsbBitStream(in_file) openMsbBitStream] and [httphttps://seed7.sourceforge.net/libraries/bitdata.htm#getBitsMsbgetBits(inout_fileinout_msbBitStream,inout_integer,in_var_integerin_integer) getBitsMsbgetBits] are used.
 
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
Line 3,391 ⟶ 3,517:
const proc: finishWriteAscii (inout file: outFile, inout integer: bitPos) is func
begin
putBitsMsb(outFile, bitPos, 0, 7); # Write a terminating NUL char.
write(outFile, chr(ord(outFile.bufferChar)));
end func;
 
const procfunc string: initReadAsciireadAscii (inout file: outFile, inout integermsbBitStream: bitPosaBitStream) is func
begin
bitPos := 8;
end func;
 
const func string: readAscii (inout file: inFile, inout integer: bitPos, in integer: length) is func
result
var string: stri is "";
Line 3,405 ⟶ 3,527:
var char: ch is ' ';
begin
while not eof(inFile) and length(stri)ch <> length'\0;' do
ch := chr(getBitsMsbgetBits(inFile, bitPosaBitStream, 7));
if inFile.bufferCharch <> EOF'\0;' then
stri &:= ch;
end if;
Line 3,417 ⟶ 3,539:
var file: aFile is STD_NULL;
var integer: bitPos is 0;
var msbBitStream: aBitStream is msbBitStream.value;
begin
aFile := openStrifileopenStriFile;
initWriteAscii(aFile, bitPos);
writeAscii(aFile, bitPos, "Hello, Rosetta Code!");
finishWriteAscii(aFile, bitPos);
seek(aFile, 1);
initReadAsciiaBitStream := openMsbBitStream(aFile, bitPos);
writeln(literal(readAscii(aFile, bitPos, 100aBitStream)));
end func;</syntaxhighlight>
 
Line 3,431 ⟶ 3,554:
"Hello, Rosetta Code!"
</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">package require Tcl 8.5
29

edits