protocol BitOps<Self> { ... }
Self: Unsigned
Bitwise operations on unsigned integers
Provided methods
-
Swaps the byte order (endianness) of the number.
Example
assert_eq!(0x0123u16.swap_bytes(), 0x2301);
Run this example -
Converts the number into a big-endian representation.
If the numbers of this type are represented in big-endian order on the target architecture, then this is an identity function.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.to_be(), 0x2301); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.to_be(), 0x0123);
Run this example -
Converts the number from a big-endian representation.
If the numbers of this type are represented in big-endian order on the target architecture, then this is an identity function.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.from_be(), 0x2301); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.from_be(), 0x0123);
Run this example -
Converts the number into a little-endian representation.
If the numbers of this type are represented in little-endian order on the target architecture, then this is an identity function.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.to_le(), 0x0123); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.to_le(), 0x2301);
Run this example -
Converts the number from a little-endian representation.
If the numbers of this type are represented in little-endian order on the target architecture, then this is an identity operation.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.from_le(), 0x0123); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.from_le(), 0x2301);
Run this example -
Returns the number of ones in the binary representation of the number.
Example
assert_eq!(0b10101000u8.count_ones(), 3);
Run this example -
Returns the number of zeros in the binary representation of the number.
Example
assert_eq!(0b10101000u8.count_zeros(), 5);
Run this example -
Returns the number of leading zeros in the binary representation of the number.
Example
assert_eq!(0b00010000u8.leading_zeros(), 3);
Run this example -
Returns the number of trailing zeros in the binary representation of the number.
Example
assert_eq!(0b00010000u8.trailing_zeros(), 4);
Run this example -
Returns the number of leading ones in the binary representation of the number.
Example
assert_eq!(0b11111110u8.leading_ones(), 7);
Run this example -
Returns the number of trailing ones in the binary representation of the number.
Example
assert_eq!(0b00000111u8.trailing_ones(), 3);
Run this example -
Rotates the bits to the left by
n
bits.Example
assert_eq!(0b01000111u8.rotate_left(2), 0b00011101u8);
Run this example -
Rotates the bits to the right by
n
bits.Example
assert_eq!(0b01000111u8.rotate_right(2), 0b11010001u8);
Run this example