Module malachite_base::num::logic::bit_convertible

source ·
Expand description

BitConvertible, a trait for extracting all bits from a number or constructing a number from bits.

§to_bits_asc

use malachite_base::num::logic::traits::BitConvertible;

assert_eq!(0u8.to_bits_asc(), &[]);
assert_eq!(2u16.to_bits_asc(), &[false, true]);
assert_eq!(
    123u32.to_bits_asc(),
    &[true, true, false, true, true, true, true]
);

assert_eq!(0i8.to_bits_asc(), &[]);
assert_eq!(2i16.to_bits_asc(), &[false, true, false]);
assert_eq!(
    (-123i32).to_bits_asc(),
    &[true, false, true, false, false, false, false, true]
);

§to_bits_desc

use malachite_base::num::logic::traits::BitConvertible;

assert_eq!(0u8.to_bits_desc(), &[]);
assert_eq!(2u16.to_bits_desc(), &[true, false]);
assert_eq!(
    123u32.to_bits_desc(),
    &[true, true, true, true, false, true, true]
);

assert_eq!(0i8.to_bits_desc(), &[]);
assert_eq!(2i16.to_bits_desc(), &[false, true, false]);
assert_eq!(
    (-123i32).to_bits_desc(),
    &[true, false, false, false, false, true, false, true]
);

§from_bits_asc

use malachite_base::num::logic::traits::BitConvertible;
use std::iter::empty;

assert_eq!(u8::from_bits_asc(empty()), 0);
assert_eq!(u16::from_bits_asc([false, true, false].iter().cloned()), 2);
assert_eq!(
    u32::from_bits_asc([true, true, false, true, true, true, true].iter().cloned()),
    123
);

assert_eq!(i8::from_bits_asc(empty()), 0);
assert_eq!(i16::from_bits_asc([false, true, false].iter().cloned()), 2);
assert_eq!(
    i32::from_bits_asc(
        [true, false, true, false, false, false, false, true]
            .iter()
            .cloned()
    ),
    -123
);

§from_bits_desc

use malachite_base::num::logic::traits::BitConvertible;
use std::iter::empty;

assert_eq!(u8::from_bits_desc(empty()), 0);
assert_eq!(u16::from_bits_desc([false, true, false].iter().cloned()), 2);
assert_eq!(
    u32::from_bits_desc([true, true, true, true, false, true, true].iter().cloned()),
    123
);

assert_eq!(i8::from_bits_desc(empty()), 0);
assert_eq!(i16::from_bits_desc([false, true, false].iter().cloned()), 2);
assert_eq!(
    i32::from_bits_desc(
        [true, false, false, false, false, true, false, true]
            .iter()
            .cloned()
    ),
    -123
);