Qu

write_bit

Builtin index · File I/O

write_bit(f, b)

Input: f, b · Output: no output

What it is

Writes one bit to f (file handle, opened writable/appendable). b (number) must be 0 or 1, accumulated MSB-first into a pending byte and flushed to disk every 8 bits (close(f) flushes any final partial byte, zero-padded).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
write_bit(f, b)
f —Writes one bit to f (file handle, opened writable/appendable).
b numberb (number) must be 0 or 1, accumulated MSB-first into a pending byte and flushed to disk every 8 bits (close(f) flushes any final partial byte, zero-padded).
no output—

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

f = fopen("flags.bin", "wb")
write_bit(f, 1)
write_bit(f, 0)
write_bit(f, 1)
write_bit(f, 1)
close(f)                       # only 4 bits written -- flushed as one padded byte


g = fopen("flags.bin", "rb")
b0 = read_bit(g)
b1 = read_bit(g)
b2 = read_bit(g)
b3 = read_bit(g)
close(g)
print("{b0}{b1}{b2}{b3}")      # 1011

print(file_size("flags.bin"))  # 1 -- one byte on disk for 4 bits

output
1011
1

from file-io

How it works

write_bit(f, b) — one bit (0 or 1), MSB-first, accumulated into a pending byte and flushed to the file every 8 bits (close(f) flushes any final partial byte, zero-padded).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.