Expand description

Implementations of PowerOf2Digits, a trait for extracting base-$2^k$ digits from Naturals and constructing Naturals from such digits.

to_power_of_2_digits_asc

extern crate malachite_base;

use malachite_base::num::basic::traits::{Two, Zero};
use malachite_base::num::conversion::traits::PowerOf2Digits;
use malachite_nz::natural::Natural;

assert_eq!(
    PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&Natural::ZERO, 6),
    Vec::<u64>::new()
);
assert_eq!(PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&Natural::TWO, 6), vec![2]);

// 123_10 = 173_8
assert_eq!(
    PowerOf2Digits::<u16>::to_power_of_2_digits_asc(&Natural::from(123u32), 3),
    vec![3, 7, 1]
);

to_power_of_2_digits_desc

extern crate malachite_base;

use malachite_base::num::basic::traits::{Two, Zero};
use malachite_base::num::conversion::traits::PowerOf2Digits;
use malachite_nz::natural::Natural;

assert_eq!(
    PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&Natural::ZERO, 6),
    Vec::<u64>::new()
);
assert_eq!(PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&Natural::TWO, 6), vec![2]);

// 123_10 = 173_8
assert_eq!(
    PowerOf2Digits::<u16>::to_power_of_2_digits_desc(&Natural::from(123u32), 3),
    vec![1, 7, 3]
);

from_power_of_2_digits_asc

extern crate malachite_base;

use malachite_base::num::conversion::traits::PowerOf2Digits;
use malachite_base::strings::ToDebugString;
use malachite_nz::natural::Natural;

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

from_power_of_2_digits_desc

extern crate malachite_base;

use malachite_base::num::conversion::traits::PowerOf2Digits;
use malachite_base::strings::ToDebugString;
use malachite_nz::natural::Natural;

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