Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,65 @@ macro_rules! int_impl {
#[stable(feature = "int_bits_const", since = "1.53.0")]
pub const BITS: u32 = <$UnsignedT>::BITS;

/// Tests the value of a specific bit.
///
/// # Examples
///
/// ```
/// #![feature(get_set_bit)]
///
#[doc = concat!("let n = 0b1000001", stringify!($SelfT), ";")]
///
/// assert!(n.bit(0));
/// assert!(!n.bit(1));
/// assert!(!n.bit(2));
/// assert!(!n.bit(3));
/// assert!(!n.bit(4));
/// assert!(!n.bit(5));
/// assert!(n.bit(6));
/// ```
#[unstable(feature = "get_set_bit", issue = "147702")]
#[rustc_const_unstable(feature = "get_set_bit", issue = "147702")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn bit(self, index: u32) -> bool {
self & (1 as $SelfT) << index != (0 as $SelfT)
}

/// Sets the value of a specific bit.
///
/// # Examples
///
/// ```
/// #![feature(get_set_bit)]
///
#[doc = concat!("let mut n = 0b1010101", stringify!($SelfT), ";")]
///
/// n = n.set_bit(0, false);
/// n = n.set_bit(1, true);
/// n = n.set_bit(2, false);
/// n = n.set_bit(3, true);
/// n = n.set_bit(4, false);
/// n = n.set_bit(5, true);
/// n = n.set_bit(6, false);
///
/// assert_eq!(n, 0b0101010);
/// ```
#[unstable(feature = "get_set_bit", issue = "147702")]
#[rustc_const_unstable(feature = "get_set_bit", issue = "147702")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn set_bit(mut self, index: u32, value: bool) -> Self {
self &= !((1 as $SelfT) << index);
self |= (value as $SelfT) << index;

self
}

/// Returns the number of ones in the binary representation of `self`.
///
/// # Examples
Expand Down
61 changes: 61 additions & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,67 @@ macro_rules! uint_impl {
#[stable(feature = "int_bits_const", since = "1.53.0")]
pub const BITS: u32 = Self::MAX.count_ones();

/// Tests the value of a specific bit.
///
/// # Examples
///
/// ```
/// #![feature(get_set_bit)]
///
#[doc = concat!("let n = 0b10000001", stringify!($SelfT), ";")]
///
/// assert!(n.bit(0));
/// assert!(!n.bit(1));
/// assert!(!n.bit(2));
/// assert!(!n.bit(3));
/// assert!(!n.bit(4));
/// assert!(!n.bit(5));
/// assert!(!n.bit(6));
/// assert!(n.bit(7));
/// ```
#[unstable(feature = "get_set_bit", issue = "147702")]
#[rustc_const_unstable(feature = "get_set_bit", issue = "147702")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn bit(self, index: u32) -> bool {
self & (1 as $SelfT) << index != (0 as $SelfT)
}

/// Sets the value of a specific bit.
///
/// # Examples
///
/// ```
/// #![feature(get_set_bit)]
///
#[doc = concat!("let mut n = 0b01010101", stringify!($SelfT), ";")]
///
/// n = n.set_bit(0, false);
/// n = n.set_bit(1, true);
/// n = n.set_bit(2, false);
/// n = n.set_bit(3, true);
/// n = n.set_bit(4, false);
/// n = n.set_bit(5, true);
/// n = n.set_bit(6, false);
/// n = n.set_bit(7, true);
///
/// assert_eq!(n, 0b10101010);
/// ```
#[unstable(feature = "get_set_bit", issue = "147702")]
#[rustc_const_unstable(feature = "get_set_bit", issue = "147702")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn set_bit(mut self, index: u32, value: bool) -> Self {
self &= !((1 as $SelfT) << index);
self |= (value as $SelfT) << index;

self
}

/// Returns the number of ones in the binary representation of `self`.
///
/// # Examples
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-86820.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::BitAnd;

const C: fn() = || is_set();
fn is_set() {
0xffu8.bit::<0>();
Bits::bit::<0>(0xffu8);
}

trait Bits {
Expand Down
Loading