Skip to content

Instantly share code, notes, and snippets.

@rizo
Created March 29, 2020 21:07
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 rizo/7b433d4a6d6a19ff20c9b51157b50405 to your computer and use it in GitHub Desktop.
Save rizo/7b433d4a6d6a19ff20c9b51157b50405 to your computer and use it in GitHub Desktop.
let int_to_bin_byte d =
if d < 0 then invalid_arg "bin_of_int" else
if d = 0 then "0" else
let rec aux acc d =
if d = 0 then acc else
aux (string_of_int (d land 1) :: acc) (d lsr 1)
in
let res = String.concat "" (aux [] d) in
if String.length res mod 8 <> 0 then
String.make (8 - String.length res mod 8) '0' ^ res
else res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment