Debian Codegraph @

debian/rust-vendor/bitvec/doc/field/BitField_load_le.md

raw
1 # Little-Endian Integer Loading
2
3 This method loads an integer value from a bit-slice, using little-endian
4 significance ordering when the bit-slice spans more than one `T` element in
5 memory.
6
7 Little-endian significance ordering means that if a bit-slice occupies an array
8 `[A, B, C]`, then the bits stored in `A` are considered to contain the least
9 significant segment of the loaded integer, then `B` contains the middle segment,
10 and then `C` contains the most significant segment.
11
12 The segments are combined in order, that is, as the raw bit-pattern
13 `0b<padding><C><B><A>`. If the destination type is signed, the loaded value is
14 sign-extended according to the most-significant bit in the `C` segment.
15
16 It is important to note that the `O: BitOrder` parameter of the bit-slice from
17 which the value is loaded **does not** affect the bit-pattern of the stored
18 segments. They are always stored exactly as they exist in an ordinary integer.
19 The ordering parameter only affects *which* bits in an element are available for
20 storage.
21
22 ## Type Parameters
23
24 - `I`: The integer type being loaded. This can be any of the signed or unsigned
25 integers.
26
27 ## Parameters
28
29 - `&self`: A bit-slice region whose length is in the range `1 ..= I::BITS`.
30
31 ## Returns
32
33 The contents of the bit-slice, interpreted as an integer.
34
35 ## Panics
36
37 This panics if `self.len()` is 0, or greater than `I::BITS`.
38
39 ## Examples
40
41 Let us consider an `i32` value stored in 24 bits of a `BitSlice<u8, Msb0>`:
42
43 ```rust
44 use bitvec::prelude::*;
45
46 let mut raw = [0u8; 4];
47 let bits = raw.view_bits_mut::<Msb0>();
48
49 let integer = 0x00__B4_96_3Cu32 as i32;
50 bits[4 .. 28].store_le::<i32>(integer);
51 let loaded = bits[4 .. 28].load_le::<i32>();
52 assert_eq!(loaded, 0xFF__B4_96_3Cu32 as i32);
53 ```
54
55 Observe that, because the lowest 24 bits began with the pattern `0b1101…`, the
56 value was considered to be negative when interpreted as an `i24` and was
57 sign-extended through the highest byte.
58
59 Let us now look at the memory representation of this value:
60
61 ```rust
62 # use bitvec::prelude::*;
63 # let mut raw = [0u8; 4];
64 # let bits = raw.view_bits_mut::<Msb0>();
65 # bits[4 .. 28].store_le::<u32>(0x00B4963Cu32);
66 assert_eq!(raw, [
67 0b0000_1100,
68 // dead 0xC
69 0b0110_0011,
70 // 0x6 0x3
71 0b0100_1001,
72 // 0x4 0x9
73 0b1011_0000,
74 // 0xB dead
75 ]);
76 ```
77
78 Notice how while the `Msb0` bit-ordering means that indexing within the
79 bit-slice proceeds left-to-right in each element, and the bit-patterns in each
80 element proceed left-to-right in the aggregate and the decomposed literals, the
81 ordering of the elements is reversed from how the literal was written.
82
83 In the sequence `B496`, `B` is the most significant, and so it gets placed
84 highest in memory. `49` fits in one byte, and is stored directly as written.
85 Lastly, `6` is the least significant nibble of the four, and is placed lowest
86 in memory.
87
88 Now let’s look at the way different `BitOrder` parameters interpret the
89 placement of bit indices within memory:
90
91 ```rust
92 use bitvec::prelude::*;
93
94 let raw = [
95 // Bit index 14 ←
96 // Lsb0: ─┤
97 0b0100_0000_0000_0011u16,
98 // Msb0: ├─
99 // → 14
100
101 // Bit index ← 19 16
102 // Lsb0: ├──┤
103 0b0001_0000_0000_1110u16,
104 // Msb0: ├──┤
105 // 16 19 →
106 ];
107
108 assert_eq!(
109 raw.view_bits::<Lsb0>()
110 [14 .. 20]
111 .load_le::<u8>(),
112 0b00_1110_01,
113 );
114 assert_eq!(
115 raw.view_bits::<Msb0>()
116 [14 .. 20]
117 .load_le::<u8>(),
118 0b00_0001_11,
119 );
120 ```
121
122 Notice how the bit-orderings change which *parts* of the memory are loaded, but
123 in both cases the segment in `raw[0]` is less significant than the segment in
124 `raw[1]`, and the ordering of bits *within* each segment are unaffected by the
125 bit-ordering.
126
127 ## Notes
128
129 Be sure to see the documentation for
130 [`<BitSlice<_, Lsb0> as BitField>::load_le`][lll] and
131 [`<BitSlice<_, Msb0> as Bitfield>::load_le`][mll] for more detailed information
132 on the memory views!
133
134 You can view the mask of all *storage regions* of a bit-slice by using its
135 [`.domain()`] method to view the breakdown of its memory region, then print the
136 [`.mask()`] of any [`PartialElement`] the domain contains. Whole elements are
137 always used in their entirety. You should use the `domain` module’s types
138 whenever you are uncertain of the exact locations in memory that a particular
139 bit-slice governs.
140
141 [lll]: https://docs.rs/bitvec/latest/bitvec/field/trait.BitField.html#method.load_le-3
142 [mll]: https://docs.rs/bitvec/latest/bitvec/field/trait.BitField.html#method.load_le-4
143 [`PartialElement`]: crate::domain::PartialElement
144 [`.domain()`]: crate::slice::BitSlice::domain
145 [`.mask()`]: crate::domain::PartialElement::mask