Skip to content

Instantly share code, notes, and snippets.

@dannywillems
Created April 23, 2021 12:15
Show Gist options
  • Save dannywillems/b2bcebd1f972bd33d76bae387cfe2d8e to your computer and use it in GitHub Desktop.
Save dannywillems/b2bcebd1f972bd33d76bae387cfe2d8e to your computer and use it in GitHub Desktop.
Print bits le in OCaml
let print_bits_le b =
assert (b >= 0 && b <= 255) ;
let rec get_bits_le x acc i =
if i = 8 then acc
else
let acc = (x mod 2) :: acc in
get_bits_le (x lsr 1) acc (i + 1)
in
let bits_le = List.rev (get_bits_le b [] 0) in
List.iter print_int bits_le ;
print_endline ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment