-
Notifications
You must be signed in to change notification settings - Fork 22
ACP: Add {u8,u16,u32,u64,u128,usize}::bit_len
#598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note that these two are arbitrary-sized integers, more like |
<$Uint>::bit_len
{u8, u16, u32, u64, u128, usize}::bit_len
{u8, u16, u32, u64, u128, usize}::bit_len
{u8,u16,u32,u64,u128,usize}::bit_len
We discussed this in the @rust-lang/libs-api meeting. We're happy to add this under the name Please open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. You can close this ACP once the tracking issue has been created. |
Thanks. I've opened the tracking issue rust-lang/rust#142326. |
…, r=tgross35 feat: Add `bit_width` for unsigned integer types - Accepted ACP: rust-lang/libs-team#598 - Tracking issue: rust-lang#142326 This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
…, r=tgross35 feat: Add `bit_width` for unsigned integer types - Accepted ACP: rust-lang/libs-team#598 - Tracking issue: rust-lang#142326 This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
…, r=tgross35 feat: Add `bit_width` for unsigned integer types - Accepted ACP: rust-lang/libs-team#598 - Tracking issue: rust-lang#142326 This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
…, r=tgross35 feat: Add `bit_width` for unsigned integer types - Accepted ACP: rust-lang/libs-team#598 - Tracking issue: rust-lang#142326 This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
Rollup merge of #142328 - sorairolake:feature/uint-bit-width, r=tgross35 feat: Add `bit_width` for unsigned integer types - Accepted ACP: rust-lang/libs-team#598 - Tracking issue: #142326 This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
feat: Add `bit_width` for unsigned integer types - Accepted ACP: rust-lang/libs-team#598 - Tracking issue: rust-lang/rust#142326 This PR adds methods to the primitive unsigned integer types that return the minimum number of bits required to represent an unsigned integer.
Uh oh!
There was an error while loading. Please reload this page.
Proposal
Problem statement
Currently, Rust does not have a method that return the minimum number of bits required to represent an integer. I think it would be useful to have a method that can do this easily and directly.
I think the bit length of unsigned integers is always clear, since these are never negative integers. I don't think it should be implemented for signed integers, because the bit length of negative integers is not clear (include the sign bit or not).
Methods for this purpose appear to exist in the standard library of several languages:
int.bit_length()
in Pythonbits.Len()
in GoInteger.bit_length
in Rubystdc_bit_width
in C23Note
int.bit_length()
in Python andInteger.bit_length
in Ruby have the same purpose as this proposal, but for arbitrary-sized integers (not just for unsigned integers).There may be a better name for this method.
Motivating examples or use cases
The following code calculates the coded dictionary size of the lzip compressed format from the dictionary size of LZMA:
Solution sketch
Alternatives
If you use existing APIs, you can calculate the bit length in the following:
I think these are simple enough, but it would be easier to understand if there is a method that can be calculated directly.
In particular, for people who often use other languages rather than Rust, I think it would be useful to have a method to directly calculate the bit length. Because, as I have already mentioned, other languages have methods to directly calculate the bit length.
Links and related work
ilog2
andchecked_ilog2
can be used to get the number of bits in an integer rust#133101What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: