Bitwise IO: Difference between revisions
Content added Content deleted
m (→{{header|Go}}: typo) |
|||
Line 1,451: | Line 1,451: | ||
69</lang> |
69</lang> |
||
Note: this implementation writes the bytes to the session (which is to say, it just gets displayed like any other result. Also, the compressed result is represented as bits - like 1 0 1 0 1... You'll of course need other code when you want to do other things.) |
Note: this implementation writes the bytes to the session (which is to say, it just gets displayed like any other result. Also, the compressed result is represented as bits - like 1 0 1 0 1... You'll of course need other code when you want to do other things.) |
||
=={{header|Julia}}== |
|||
ASCII 7-bit character string compression and decompression, to demonstrate bit twiddling. Implemented as generic IO, so that file handles are usable with the same functions. |
|||
<lang Julia> |
|||
function compress7(inio, outio) |
|||
nextwritebyte = read(inio, UInt8) & 0x7f |
|||
filled = 7 |
|||
while !eof(inio) |
|||
inbyte = read(inio, UInt8) |
|||
write(outio, UInt8(nextwritebyte | inbyte << filled)) |
|||
nextwritebyte = inbyte >> (8 - filled) |
|||
filled = (filled + 7) % 8 |
|||
if filled == 0 |
|||
if eof(inio) |
|||
break |
|||
end |
|||
nextwritebyte = read(inio, UInt8) & 0x7f |
|||
filled = 7 |
|||
end |
|||
end |
|||
if filled != 0 |
|||
write(outio, UInt8(nextwritebyte)) |
|||
end |
|||
end |
|||
function expand7(inio, outio) |
|||
newbyte = read(inio, UInt8) |
|||
write(outio, UInt8(newbyte & 0x7f)) |
|||
residualbyte::UInt8 = newbyte >> 7 |
|||
filled = 1 |
|||
while !eof(inio) |
|||
inbyte = read(inio, UInt8) |
|||
write(outio, UInt8((residualbyte | inbyte << filled) & 0x7f)) |
|||
residualbyte = inbyte >> (7 - filled) |
|||
filled = (filled + 1) % 7 |
|||
if filled == 0 |
|||
write(outio, UInt8(residualbyte & 0x7f)) |
|||
residualbyte = 0 |
|||
end |
|||
end |
|||
end |
|||
str = b"These bit oriented I/O functions can be used to implement compressors and decompressors." |
|||
ins = IOBuffer(str) |
|||
outs = IOBuffer() |
|||
newouts = IOBuffer() |
|||
compress7(ins, outs) |
|||
seek(outs,0) |
|||
expand7(outs, newouts) |
|||
println("Initial string of length $(length(str)): ", String(ins.data)) |
|||
println("Compressed to length $(length(outs.data)): ", outs.data) |
|||
println("Decompressed string: ", String(newouts.data)) |
|||
{{data}}<pre> |
|||
Initial string of length 88: These bit oriented I/O functions can be used to implement compressors and decompressors. |
|||
Compressed to length 77: UInt8[0x54, 0x74, 0x79, 0x5e, 0x06, 0x89, 0xd3, 0x74, 0xd0, 0x5b, 0x9e, 0x2e, 0xbb, 0xe9, 0x65, 0x32, 0x28, 0xf9, 0x7a, 0x82, 0xcc, 0x75, 0xf7, 0x98, 0x9e, 0x7e, 0xbb, 0xe7, 0xa0, 0x71, 0xd8, 0x0d, 0x12, 0x97, 0x41, 0xf5, 0x79, 0x99, 0x0c, 0xa2, 0xbf, 0x41, 0xe9, 0x36, 0x9c, 0x5d, 0x6e, 0x97, 0xdd, 0x74, 0xd0, 0xf8, 0xdd, 0x86, 0xcb, 0xcb, 0xf3, 0xf9, 0x5b, 0x3e, 0x07, 0x85, 0xdd, 0x64, 0x10, 0xb9, 0x3c, 0x7e, 0xb7, 0xe1, 0xf2, 0xf2, 0x7c, 0xfe, 0x96, 0xcf, 0x5d] |
|||
Decompressed string: These bit oriented I/O functions can be used to implement compressors and decompressors.</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |