Skip to content

Implement bit and set_bit for integral types. - #147696

Open
bjoernager wants to merge 1 commit into
rust-lang:mainfrom
bjoernager:bit
Open

bjoernager wants to merge 1 commit into
rust-lang:mainfrom
bjoernager:bit

Conversation

@bjoernager

@bjoernager bjoernager commented Oct 14, 2025

Copy link
Copy Markdown
Contributor

View all comments

Tracking issue: #147702

This PR implements the bit and set_bit methods for integral types:

impl u8 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u16 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u32 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u64 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl u128 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl usize {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i8 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i16 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i32 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i64 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl i128 {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

impl isize {
    pub const fn bit(self, index: u32) -> bool;

    pub const fn set_bit(self, index: u32, value: bool) -> Self;
}

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

rustbot commented Oct 14, 2025

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

@bjoernager

bjoernager commented Oct 14, 2025

Copy link
Copy Markdown
Contributor Author

@rustbot label +T-libs-api -T-compiler -T-libs

r? libs-api

@rustbot rustbot added the T-libs-api [DEPRECATED; DO NOT USE] label Oct 14, 2025
@rustbot rustbot assigned dtolnay and unassigned Mark-Simulacrum Oct 14, 2025
@rustbot rustbot removed T-libs Relevant to the library team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 14, 2025
@hanna-kruppe

hanna-kruppe commented Oct 14, 2025

Copy link
Copy Markdown
Contributor

Not sure if this detail was discussed on the ACP, but the name set_bit reads to me like its signature should be (&mut self, i: u32, value: bool) -> () and not (as implemented here) (self, i: u32, value: bool) -> Self, which I'd call with_bit_set or with_bit.

@bjoernager bjoernager changed the title Implement bit and set_bit for integral types; Implement bit and set_bit for integral types. Oct 14, 2025
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
@bjoernager

Copy link
Copy Markdown
Contributor Author

Not sure if this detail was discussed on the ACP, but the name set_bit reads to me like its signature should be (&mut self, i: u32) -> () and not (as implemented here) (self, i: u32) -> Self, which I'd call with_bit_set.

Or perhaps just with_bit (given that value does not need to be set)? But I'm inclined to agree.

@hanna-kruppe

hanna-kruppe commented Oct 14, 2025

Copy link
Copy Markdown
Contributor

I glossed over the value argument entirely when I wrote my first comment (edited now). Once I've noticed value I also have questions about it. To be honest, skimming the ACP discussion again, it seems like the setter half wasn't really clearly defined before being given a name and stamp of approval (e.g., two comments by @bjoernager disagreed over whether set_bit has a value parameter or not).

@bjoernager

bjoernager commented Oct 14, 2025

Copy link
Copy Markdown
Contributor Author

two comments by @bjoernager disagreed over whether set_bit has a value parameter or not).

My latter comment should've had the value parameter as well (fixed now).

Comment thread library/core/src/num/int_macros.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@bjoernager
bjoernager force-pushed the bit branch 2 times, most recently from 00e99a8 to de2e411 Compare October 14, 2025 19:41
@rust-log-analyzer

This comment has been minimized.

@AljoschaMeyer

Copy link
Copy Markdown
Contributor

Thank you @bjoernager for implementing (and probably championing) this 💜.

Fwiw I also think with_bit is probably the better name. If the doc comments of bit included a "see also with_bit" (and vice versa, for consistency), that could help people find the setter despite the perhaps initially unexpected name.

@rust-log-analyzer

This comment has been minimized.

@bjoernager

Copy link
Copy Markdown
Contributor Author

Suggestions seemed very rational to me and have all been implemented. Let me know if there are any other issues with the implementation, @dtolnay. :D

@bjoernager

This comment was marked as outdated.

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to implement more test coverage in library/coretests/tests/num/int_macros.rs and /library/coretests/tests/num/uint_macros.rs. Don't forget to add the feature to library/coretests/tests/lib.rs.

These are also good suggestions that haven't been implemented yet (or not pushed?). The doc tests cover the logic but don't test e.g. const evaluation

View changes since this review

Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/uint_macros.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2026
@rustbot

rustbot commented Apr 18, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

rustbot commented May 20, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@bjoernager

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 20, 2026
@rust-log-analyzer

This comment has been minimized.

@bebecue

bebecue commented May 26, 2026

Copy link
Copy Markdown
Contributor

The <$int>::bit() function is useful on its own and has received concerns so far. Shall we split this feature into two, like int_get_bit and int_set_bit? while stabilizing int_get_bit, int_set_bit could still receive further improvements. What do you think?

@clarfonthey clarfonthey added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-libs-api [DEPRECATED; DO NOT USE] labels Aug 12, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

r? libs

@rustbot rustbot assigned clarfonthey and unassigned dtolnay Sep 16, 2026
without modifying the original"]
#[track_caller]
pub const fn bit(self, index: u32) -> bool {
self & (1 as $SelfT) << index != (0 as $SelfT)

@clarfonthey clarfonthey Sep 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to just call self.cast_unsigned().bit(index) instead of rewriting the logic, to avoid duplicating the code.

View changes since the review

Comment on lines +128 to +131
self &= !((1 as $SelfT) << index);
self |= (value as $SelfT) << index;

self

@clarfonthey clarfonthey Sep 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly would expect self.cast_unsigned().set_bit(index, value)

View changes since the review

without modifying the original"]
#[track_caller]
pub const fn bit(self, index: u32) -> bool {
self & (1 as $SelfT) << index != (0 as $SelfT)

@clarfonthey clarfonthey Sep 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that operator precedence is on your side but parentheses help make this clearer:

            (self & ((1 as $SelfT) << index)) != (0 as $SelfT)

I'm also not entirely sure the as $SelfT is really necessary here:

Suggested change
self & (1 as $SelfT) << index != (0 as $SelfT)
(self & (1 << index)) != 0

View changes since the review

Comment on lines +134 to +137
self &= !((1 as $SelfT) << index);
self |= (value as $SelfT) << index;

self

@clarfonthey clarfonthey Sep 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, don't think you need the 1 as $SelfT:

Suggested change
self &= !((1 as $SelfT) << index);
self |= (value as $SelfT) << index;
self
self &= !(1 << index);
self |= (value as $SelfT) << index;
self

View changes since the review

@clarfonthey

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.