Logo

Programming-Idioms

  • Fortran
  • Rust

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

let s = format!("{x:b}");
let s = format!("{:b}", x);

Formatting lets you choose another representation (e.g., `b` for binary, `X` for hex in upper case and `?` for debug output).
write (unit=s,fmt='(B32)') x

Pads with zeros
write (unit=s,fmt='(B0)') x
(:require [clojure.pprint])
(defn s
  [x]
  (pprint/cl-format nil "~b" x))

Call s and pass an integer x to it and it will return your integer in base 2.

New implementation...