Skip to main content

Module shr

Module shr 

Source
Expand description

Right-shifting a vector (dividing it by a power of 2 and taking the floor), by implementations of Shr and ShrAssign.

§shr

use core::str::FromStr;
use malachite_base::unsigned_vector::UnsignedVector;

assert_eq!(
    (UnsignedVector::<u8>::from_str("()").unwrap() >> 10u8).to_string(),
    "()"
);
assert_eq!(
    (UnsignedVector::<u8>::from_str("(1, 2, 3, 5)").unwrap() >> 0u16).to_string(),
    "(1, 2, 3, 5)"
);
assert_eq!(
    (UnsignedVector::<u8>::from_str("(1, 2, 3, 5)").unwrap() >> 1u32).to_string(),
    "(0, 1, 1, 2)"
);
assert_eq!(
    (&UnsignedVector::<u8>::from_str("(255, 128)").unwrap() >> 7u64).to_string(),
    "(1, 1)"
);
// Shifting by at least the width zeroes every element, keeping the dimension.
assert_eq!(
    (&UnsignedVector::<u8>::from_str("(255, 128)").unwrap() >> 8u128).to_string(),
    "(0, 0)"
);
assert_eq!(
    (UnsignedVector::<u64>::from_str("(18446744073709551615, 1)").unwrap() >> 63usize)
        .to_string(),
    "(1, 0)"
);

§shr_assign

use core::str::FromStr;
use malachite_base::unsigned_vector::UnsignedVector;

let mut v = UnsignedVector::<u8>::from_str("(1, 2, 3, 5)").unwrap();
v >>= 1u8;
assert_eq!(v.to_string(), "(0, 1, 1, 2)");

let mut v = UnsignedVector::<u8>::from_str("(255, 128)").unwrap();
v >>= 200u32;
assert_eq!(v.to_string(), "(0, 0)");