Skip to content

Instantly share code, notes, and snippets.

@MagnificentPako
Created March 19, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagnificentPako/37d8fcecd3b1f26f0100427f9bec8591 to your computer and use it in GitHub Desktop.
Save MagnificentPako/37d8fcecd3b1f26f0100427f9bec8591 to your computer and use it in GitHub Desktop.
class BitWriter
fun _bit_to_bool(bit: U8): Bool =>
if bit == 0 then false else true end
fun byte_to_bits(byte': U8): Array[U8] =>
var bits: Array[U8] = bits.create(8)
var mask: U8 = 0b00000001
bits.push(((byte' >> 7) and mask))
bits.push(((byte' >> 6) and mask))
bits.push(((byte' >> 5) and mask))
bits.push(((byte' >> 4) and mask))
bits.push(((byte' >> 3) and mask))
bits.push(((byte' >> 2) and mask))
bits.push(((byte' >> 1) and mask))
bits.push((byte' and mask))
bits
fun bits_to_byte(bits: Array[U8]): U8 =>
var res: U8 = 0
try
res = (bits.pop()) or
(bits.pop() << 1) or
(bits.pop() << 2) or
(bits.pop() << 3) or
(bits.pop() << 4) or
(bits.pop() << 5) or
(bits.pop() << 6) or
(bits.pop() << 7)
end
res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment