How to print an integer in binary format with leading zeros?

I am doing a little hide and I would like to print all the bits in my u16.

let flags = 0b0000000000101100u16;
println!("flags: {:#b}", flags);

Will print flags: 0b101100.

How do I print flags: 0b0000000000101100?

+4
source share
1 answer
let flags = 0b0000000000101100u16;
println!("flags: {:#018b}", flags);

Gaskets 018with zeros up to a width of 18. This width includes 0b(length = 2) plus u16 (length = 16), therefore 18 = 2 + 16. It should be between # and b.

Rust fmt docs explain both leading zeros and radix formatting , but don't show how to combine them.

+6
source

Source: https://habr.com/ru/post/1679862/


All Articles