Skip to content

Commit 24fb4b7

Browse files
committed
Add reverse_bits to integer types
1 parent 02e021b commit 24fb4b7

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/libcore/num/mod.rs

+54
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,33 @@ $EndFeature, "
321321
(self as $UnsignedT).swap_bytes() as Self
322322
}
323323

324+
/// Reverses the bit pattern of the integer.
325+
///
326+
/// # Examples
327+
///
328+
/// Please note that this example is shared between integer types.
329+
/// Which explains why `i16` is used here.
330+
///
331+
/// Basic usage:
332+
///
333+
/// ```
334+
/// #![feature(reverse_bits)]
335+
///
336+
/// let n: i16 = 0b0000000_01010101;
337+
/// assert_eq!(n, 85);
338+
///
339+
/// let m = n.reverse_bits();
340+
///
341+
/// assert_eq!(m as u16, 0b10101010_00000000);
342+
/// assert_eq!(m, -22016);
343+
/// ```
344+
#[unstable(feature = "reverse_bits", issue = "48763")]
345+
#[cfg(not(stage0))]
346+
#[inline]
347+
pub fn reverse_bits(self) -> Self {
348+
(self as $UnsignedT).reverse_bits() as Self
349+
}
350+
324351
doc_comment! {
325352
concat!("Converts an integer from big endian to the target's endianness.
326353
@@ -1773,6 +1800,33 @@ assert_eq!(n.trailing_zeros(), 3);", $EndFeature, "
17731800
unsafe { intrinsics::bswap(self as $ActualT) as Self }
17741801
}
17751802

1803+
/// Reverses the bit pattern of the integer.
1804+
///
1805+
/// # Examples
1806+
///
1807+
/// Basic usage:
1808+
///
1809+
/// Please note that this example is shared between integer types.
1810+
/// Which explains why `u16` is used here.
1811+
///
1812+
/// ```
1813+
/// #![feature(reverse_bits)]
1814+
///
1815+
/// let n: u16 = 0b0000000_01010101;
1816+
/// assert_eq!(n, 85);
1817+
///
1818+
/// let m = n.reverse_bits();
1819+
///
1820+
/// assert_eq!(m, 0b10101010_00000000);
1821+
/// assert_eq!(m, 43520);
1822+
/// ```
1823+
#[unstable(feature = "reverse_bits", issue = "48763")]
1824+
#[cfg(not(stage0))]
1825+
#[inline]
1826+
pub fn reverse_bits(self) -> Self {
1827+
unsafe { intrinsics::bitreverse(self as $ActualT) as Self }
1828+
}
1829+
17761830
doc_comment! {
17771831
concat!("Converts an integer from big endian to the target's endianness.
17781832

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#![feature(try_trait)]
4747
#![feature(exact_chunks)]
4848
#![feature(atomic_nand)]
49+
#![feature(reverse_bits)]
4950

5051
extern crate core;
5152
extern crate test;

src/libcore/tests/num/uint_macros.rs

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ mod tests {
9797
assert_eq!(_1.swap_bytes(), _1);
9898
}
9999

100+
#[test]
101+
fn test_reverse_bits() {
102+
assert_eq!(A.reverse_bits().reverse_bits(), A);
103+
assert_eq!(B.reverse_bits().reverse_bits(), B);
104+
assert_eq!(C.reverse_bits().reverse_bits(), C);
105+
106+
// Swapping these should make no difference
107+
assert_eq!(_0.reverse_bits(), _0);
108+
assert_eq!(_1.reverse_bits(), _1);
109+
}
110+
100111
#[test]
101112
fn test_le() {
102113
assert_eq!($T::from_le(A.to_le()), A);

0 commit comments

Comments
 (0)