Skip to main content

ConstPowerOfTwo

Trait ConstPowerOfTwo 

Source
pub trait ConstPowerOfTwo:
    Sized
    + ConstZero
    + ConstOne {
    // Required methods
    fn is_power_of_two(&self) -> bool;
    fn next_power_of_two(self) -> Self;
    fn checked_next_power_of_two(self) -> Option<Self>;
}
Expand description

Const-compatible power-of-two operations.

These methods mirror the inherent methods on primitive integers but are not part of num_traits.

§Unsigned types only

This trait is designed for unsigned integer types. Implementing it for signed types may produce unexpected results (e.g., negative values are never powers of two, and next_power_of_two behavior is undefined for negative inputs).

Required Methods§

Source

fn is_power_of_two(&self) -> bool

Returns true if self is a power of two.

Zero is not a power of two.

Source

fn next_power_of_two(self) -> Self

Returns the smallest power of two greater than or equal to self.

§Panics

Panics if the result overflows (i.e., self > (1 << (bits-1))).

Source

fn checked_next_power_of_two(self) -> Option<Self>

Returns the smallest power of two greater than or equal to self.

Returns None if the result would overflow.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ConstPowerOfTwo for u8

Source§

impl ConstPowerOfTwo for u16

Source§

impl ConstPowerOfTwo for u32

Source§

impl ConstPowerOfTwo for u64

Source§

impl ConstPowerOfTwo for u128

Implementors§

Source§

impl<T: ConstMachineWord + MachineWord, const N: usize> ConstPowerOfTwo for FixedUInt<T, N>