Trait bitwise::Words [] [src]

pub trait Words {
    fn count_ones(&self) -> usize;
    fn count_zeros(&self) -> usize;
    fn leading_zeros(&self) -> usize;
    fn size(&self) -> usize;
}

Bitwise manimpulation algorithms for sequences of Words.

Note: this trait is not supposed to be implemented by library users. The distinction between functionality required and inherited has been arbitrarily placed at the boundary between "what is easier to implement within the trait" and "what is easier to implement with a macro" for the primitive integer types.

Required Methods

fn count_ones(&self) -> usize

Returns the number of ones in the binary representation of self.

Examples

use bitwise::Words;

let n = 0b0100_1100u8;

assert_eq!(n.count_ones(), 3);

let ns0 = [0u8, 1u8, 0b0100_1100u8];
let ns1 = [1u64, 0u64, 0b0100_1100u64];

assert_eq!(ns0.count_ones(), 4);
assert_eq!(ns1.count_ones(), 4);

fn count_zeros(&self) -> usize

Returns the number of zeros in the binary representation of self.

Examples

use bitwise::Words;

let n = 0b0100_1100u8;

assert_eq!(n.count_zeros(), 5);

let ns = [0u8, 1u8, 0b0100_1100u8];

assert_eq!(ns.count_zeros(), 8 + 7 + 5);

fn leading_zeros(&self) -> usize

Returns the number of leading zeros in the binary representation of self.

Examples

use bitwise::Words;

let n = 0b0010_1000u16;

assert_eq!(n.leading_zeros(), 10);

let ns = [0u8, 0b0010_1000u8, 1u8];

assert_eq!(ns.leading_zeros(), 10);

fn size(&self) -> usize

Size of the word sequence.

Implementors