[−][src]Function sector_base::io::fr32::shift_bits
pub fn shift_bits(input: &[u8], amount: usize, is_left: bool) -> Vec<u8>
Shift an amount of bits from the input in the direction indicated by is_left.
This function tries to imitate the behavior of shl and shr of a
BitVec<LittleEndian, u8>, where the inner vector is traversed one byte
at a time (u8), and inside each byte, bits are traversed (LittleEndian)
from LSB ("right") to MSB ("left"). For example, the bits in the this two-byte
slice will be traversed according to their numbering:
ADDR | 7 6 5 4 3 2 1 0 |
ADDR +1 | F E D C B A 9 8 |
BitVec uses the opposite naming convention than this function, shifting left
here is equivalent to shr there, and shifting right to shl.
If shifting in the left direction, the input is expanded by one extra byte to
accommodate the overflow (instead of just discarding it, which is what's done
in the right direction).
The maximum amount to shift is 7 (and the minimum is 1), that is, we always
shift less than a byte. This precondition is only checked during testing (with
debug_assert!) for performance reasons, it is up to the caller to enforce it.
Examples
Shift the input (taken from the diagram above) left by an amount of 3 bits,
growing the output slice:
ADDR | 4 3 2 1 0 _ _ _ | Filled with zeros.
ADDR +1 | C B A 9 8 7 6 5 |
ADDR +2 | _ _ _ _ _ F E D | The overflow of the last input byte
is moved to this (new) byte.
Same, but shift right:
ADDR | A 9 8 7 6 5 4 3 | The overflow `[2,1,0]` is just discarded,
the slice doesn't grow.
ADDR +1 | _ _ _ F E D C B |
(Note: 0, 1, 2, etc. are bits identified by their original position,
_ means a bit left at zero after shifting, to avoid confusions with
the unique bit 0, that just started at that position but doesn't
necessarily carry that value.)