Logo

Programming-Idioms

History of Idiom 76 > diff from v10 to v11

Edit summary for version 11 by :

Version 10

2015-08-21, 14:49:02

Version 11

2015-08-21, 15:11:27

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);
Comments bubble
Formatting allows you to choose another representation (e.g., `b` for binary, `X` for hex in upper case and `?` for debug output).
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