Function bitfrob::u32_replicate_bits
source · pub fn u32_replicate_bits(count: u32, u: u32) -> u32
Expand description
Replicates the lowest count
bits across the entire value.
If higher bits are set in the input it will not affect the output (they end up being discarded).
This is a form of “sample depth scaling”. If your source data’s bit
depth is less than the full range of a uX
value (eg, only the lowest
5 bits in a u8
), this function can scale up the sample into the
integer’s full range.
Panics
count
can’t exceed the number of bits in the type.count
can’t be 0.
assert_eq!(u8_replicate_bits(5, 0b0001_0111_u8), 0b1011_1101_u8);
assert_eq!(u16_replicate_bits(5, 0b0001_0111_u16), 0b1011_1101_1110_1111_u16);
assert_eq!(u32_replicate_bits(5, 0b0001_0111_u32), 0xBDEF_7BDE_u32);
assert_eq!(u64_replicate_bits(5, 0b0001_0111_u64), 0xBDEF_7BDE_F7BD_EF7B_u64);
assert_eq!(u128_replicate_bits(5, 0b0001_0111_u128), 0xBDEF_7BDE_F7BD_EF7B_DEF7_BDEF_7BDE_F7BD_u128);