Skip to content

[ER] Basic bit twiddling methods #82378

Open
@leonardo-m

Description

@leonardo-m

A simple enhancement request. I think in line with the idea of Rust design to give low level control, to offer an ergonomic API that reduces the chances for bugs while keeping code efficient, and at the same time avoiding too many special operators, I think Rust stdlib could add few simple functions to work on bits of integral values (u8, i8, ...etc, usize, isize and char):

x.set_bit(7);
x.set_bit(1 ..= 3);
x.reset_bit(7);
x.reset_bit(1 .. 5);
x.invert_bit(7);
x.invert_bit(2 ..);
if x.is_bit_set(7) {} // For simplicity this accepts only single indexes.
if x.is_bit_reset(7) {}

Such functions can be implemented in user level code, in just a handful of lines, but they are twiddly and it's boring to implement them many times (especially the interval ones), so having them in the stdlib saves bugs, time and allows to focus on more interesting code :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions