Skip to content

Commit 9c6f148

Browse files
committed
PrimInt: add reverse_bits() method
1 parent e8da6fe commit 9c6f148

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/int.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ pub trait PrimInt:
218218
/// ```
219219
fn swap_bytes(self) -> Self;
220220

221+
/// Reverses the order of bits in the integer.
222+
///
223+
/// The least significant bit becomes the most significant bit, second least-significant bit
224+
/// becomes second most-significant bit, etc.
225+
///
226+
/// # Examples
227+
///
228+
/// ```
229+
/// use num_traits::PrimInt;
230+
///
231+
/// let n = 0x12345678u32;
232+
/// let m = 0x1e6a2c48u32;
233+
///
234+
/// assert_eq!(n.reverse_bits(), m);
235+
/// assert_eq!(0u32.reverse_bits(), 0);
236+
/// ```
237+
fn reverse_bits(self) -> Self;
238+
221239
/// Convert an integer from big endian to the target's endianness.
222240
///
223241
/// On big endian this is a no-op. On little endian the bytes are swapped.
@@ -364,6 +382,11 @@ macro_rules! prim_int_impl {
364382
<$T>::swap_bytes(self)
365383
}
366384

385+
#[inline]
386+
fn reverse_bits(self) -> Self {
387+
<$T>::reverse_bits(self)
388+
}
389+
367390
#[inline]
368391
fn from_be(x: Self) -> Self {
369392
<$T>::from_be(x)

0 commit comments

Comments
 (0)