Skip to content

Instantly share code, notes, and snippets.

@gsg
Created September 18, 2016 07:55
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 gsg/87795be559b34513fc4fa0ff7a8530e0 to your computer and use it in GitHub Desktop.
Save gsg/87795be559b34513fc4fa0ff7a8530e0 to your computer and use it in GitHub Desktop.
let most_significant_bit =
max_int lxor (max_int lsr 1)
let print_int_binary n =
print_string "0b";
let rec loop started bit n =
if bit = 0 then ()
else
let b = n land bit in
if b = 0 then begin
if started then print_char '0';
loop started (bit lsr 1) n
end
else begin
print_char '1';
loop true (bit lsr 1) n
end in
loop false most_significant_bit n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment