Debian Codegraph @

debian/rust-vendor/bitvec/doc/slice/format.md

raw
1 # Bit-Slice Rendering
2
3 This implementation prints the contents of a `&BitSlice` in one of binary,
4 octal, or hexadecimal. It is important to note that this does *not* render the
5 raw underlying memory! They render the semantically-ordered contents of the
6 bit-slice as numerals. This distinction matters if you use type parameters that
7 differ from those presumed by your debugger (which is usually `<u8, Msb0>`).
8
9 The output separates the `T` elements as individual list items, and renders each
10 element as a base- 2, 8, or 16 numeric string. When walking an element, the bits
11 traversed by the bit-slice are considered to be stored in
12 most-significant-bit-first ordering. This means that index `[0]` is the high bit
13 of the left-most digit, and index `[n]` is the low bit of the right-most digit,
14 in a given printed word.
15
16 In order to render according to expectations of the Arabic numeral system, an
17 element being transcribed is chunked into digits from the least-significant end
18 of its rendered form. This is most noticeable in octal, which will always have a
19 smaller ceiling on the left-most digit in a printed word, while the right-most
20 digit in that word is able to use the full `0 ..= 7` numeral range.
21
22 ## Examples
23
24 ```rust
25 # #[cfg(feature = "std")] {
26 use bitvec::prelude::*;
27
28 let data = [
29 0b000000_10u8,
30 // digits print LTR
31 0b10_001_101,
32 // significance is computed RTL
33 0b01_000000,
34 ];
35 let bits = &data.view_bits::<Msb0>()[6 .. 18];
36
37 assert_eq!(format!("{:b}", bits), "[10, 10001101, 01]");
38 assert_eq!(format!("{:o}", bits), "[2, 215, 1]");
39 assert_eq!(format!("{:X}", bits), "[2, 8D, 1]");
40 # }
41 ```
42
43 The `{:#}` format modifier causes the standard `0b`, `0o`, or `0x` prefix to be
44 applied to each printed word. The other format specifiers are not interpreted by
45 this implementation, and apply to the entire rendered text, not to individual
46 words.