Module malachite_base::num::conversion::digits::power_of_2_digits

source ·
Expand description

PowerOf2Digits, a trait for extracting base-$2^k$ $digits from numbers and constructing numbers from digits.

§to_power_of_2_digits_asc

use malachite_base::num::conversion::traits::PowerOf2Digits;

assert_eq!(
    PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&0u8, 6),
    &[]
);
assert_eq!(
    PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&2u16, 6),
    &[2]
);
// 123_10 = 173_8
assert_eq!(
    PowerOf2Digits::<u16>::to_power_of_2_digits_asc(&123u32, 3),
    &[3, 7, 1]
);

§to_power_of_2_digits_desc

use malachite_base::num::conversion::traits::PowerOf2Digits;

assert_eq!(
    PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&0u8, 6),
    &[]
);
assert_eq!(
    PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&2u16, 6),
    &[2]
);
// 123_10 = 173_8
assert_eq!(
    PowerOf2Digits::<u16>::to_power_of_2_digits_desc(&123u32, 3),
    &[1, 7, 3]
);

§from_power_of_2_digits_asc

use malachite_base::num::conversion::traits::PowerOf2Digits;

assert_eq!(
    u8::from_power_of_2_digits_asc(6, [0u64, 0, 0].iter().cloned()),
    Some(0)
);
assert_eq!(
    u16::from_power_of_2_digits_asc(6, [2u64, 0].iter().cloned()),
    Some(2)
);
assert_eq!(
    u32::from_power_of_2_digits_asc(3, [3u16, 7, 1].iter().cloned()),
    Some(123)
);

assert!(u8::from_power_of_2_digits_asc(4, [1u64; 100].iter().cloned()).is_none());
assert!(u8::from_power_of_2_digits_asc(1, [2u64].iter().cloned()).is_none());

§from_power_of_2_digits_desc

use malachite_base::num::conversion::traits::PowerOf2Digits;

assert_eq!(
    u8::from_power_of_2_digits_desc(6, [0u64, 0, 0].iter().cloned()),
    Some(0)
);
assert_eq!(
    u16::from_power_of_2_digits_desc(6, [0u64, 2].iter().cloned()),
    Some(2)
);
assert_eq!(
    u32::from_power_of_2_digits_desc(3, [1u16, 7, 3].iter().cloned()),
    Some(123)
);

assert!(u8::from_power_of_2_digits_desc(4, [1u64; 100].iter().cloned()).is_none());
assert!(u8::from_power_of_2_digits_desc(1, [2u64].iter().cloned()).is_none());