Skip to content

Conversation

AljoschaMeyer
Copy link
Contributor

Hi,

this small PR adds a small method to int and uint types in core, for easily checking whether a particular bit is set to one or not:

/// Returns whether the `k`-th least-significant bit in the binary
/// representation of `self` is a one.
///
/// Returns `false` if the supplied index is greater than or equal to the
/// number of bits of the binary representation of `self`.
///
/// # Examples
///
/// ```
#[doc = concat!("let n = 0b00000101", stringify!($SelfT), ";")]
/// assert_eq!(n.is_bit_one(0), true);
/// assert_eq!(n.is_bit_one(1), false);
/// assert_eq!(n.is_bit_one(2), true);
/// assert_eq!(n.is_bit_one(99999), false);
/// ```
#[stable(feature = "leading_trailing_ones", since = "1.46.0")]
#[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
#[inline(always)]
pub const fn is_bit_one(self, k: usize) -> bool {
    ((1 << k) & self) > 0
}

A couple of questions:

  • Is this something you'd like to see in core at all? I found myself repeatedly implementing this method in multiple projects, and to me it does feel quite at home in core with the other bit-level operations and ints and uints.
  • Is this method small enough to simply add, or does it require a feature for stability tracking (and/or even an RFC)? If so, what should my next steps be?
  • To get things to compile, I simply copy-pasted the #[stable] and #[rust_const_stable] attributes of another method. This obviously needs to change before merging. To what do I need to change it?

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 4, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer
Copy link
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_int_macros_rs_178_4
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/int_macros.rs - num::i128::is_bit_one (line 178) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_int_macros_rs_178_1
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/int_macros.rs - num::i16::is_bit_one (line 178) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_int_macros_rs_178_2
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/int_macros.rs - num::i32::is_bit_one (line 178) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_int_macros_rs_178_3
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/int_macros.rs - num::i64::is_bit_one (line 178) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_int_macros_rs_178_0
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/int_macros.rs - num::i8::is_bit_one (line 178) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_int_macros_rs_178_5
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/int_macros.rs - num::isize::is_bit_one (line 178) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_uint_macros_rs_211_4
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/uint_macros.rs - num::u128::is_bit_one (line 211) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_uint_macros_rs_211_1
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/uint_macros.rs - num::u16::is_bit_one (line 211) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_uint_macros_rs_211_2
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/uint_macros.rs - num::u32::is_bit_one (line 211) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_uint_macros_rs_211_3
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/uint_macros.rs - num::u64::is_bit_one (line 211) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_uint_macros_rs_211_0
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/uint_macros.rs - num::u8::is_bit_one (line 211) stdout end ----
---
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_const::panic_const_shl_overflow
   3: rust_out::main::_doctest_main_library_core_src_num_uint_macros_rs_211_5
   4: rust_out::main
   5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/uint_macros.rs - num::usize::is_bit_one (line 211) stdout end ----

@theemathas
Copy link
Contributor

Does this implementation work for checking if the sign bit is one?

@orlp
Copy link
Contributor

orlp commented Oct 4, 2025

I think this should go through an ACP.

Also, the implementation is broken, in debug mode it will panic for shifts greater than the width, in release mode it will wrap so 2_u32.is_bit_set(33) will return true.

@Kivooeo
Copy link
Member

Kivooeo commented Oct 4, 2025

Hi, thanks for the PR!

Before we can review this, it requires an API Change Proposal (ACP). The libs-api team uses this process to evaluate if the change is a good fit for the standard library.

Please follow the process here to get started:
https://github.com/rust-lang/libs-team/issues/new?template=api-change-proposal.md

Blocked on ACP.

@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 4, 2025
@cyrgani cyrgani added the needs-acp This change is blocked on the author creating an ACP. label Oct 4, 2025
@AljoschaMeyer
Copy link
Contributor Author

Thank you for the quick replies 💜. I did not know about ACPs before, but here we go: rust-lang/libs-team#667

Given that the logic for returning false on "out-of-bounds" indices is not entirely trivial, I've proposed in the aCP to panic on invalid indices instead of returning false. I'll wait for the ACP process before adjusting this PR though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-acp This change is blocked on the author creating an ACP. S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants