This language bar is your friend. Select your favorite languages!
Edit summary for version 12 :

Version 11

Version 12

Idiom #76 Binary digits from an integer

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

E.g. 13 -> "1101"

Idiom #76 Binary digits from an integer

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

E.g. 13 -> "1101"

Code
let s = format!("{:b}", x);
Code
let s = format!("{:b}", x);
Comments bubble
Formatting allows you to choose another representation (e.g., `b` for binary, `X` for hex in upper case and `?` for debug output).
Comments bubble
Formatting allows you to choose another representation (e.g., `b` for binary, `X` for hex in upper case and `_?` for debug output).
Doc URL
http://doc.rust-lang.org/std/fmt/index.html
Origin
http://doc.rust-lang.org/std/fmt/index.html
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20let%20s%20%3D%20format!(%22%7B%3Ab%7D%22%2C%20x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s)%3B%0A%7D&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20let%20s%20%3D%20format!(%22%7B%3Ab%7D%22%2C%20x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s)%3B%0A%7D&version=stable