Skip to content
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

Get/set n-th bit of BigUint and BigInt #183

Merged
merged 18 commits into from Feb 24, 2021
Merged

Get/set n-th bit of BigUint and BigInt #183

merged 18 commits into from Feb 24, 2021

Conversation

janmarthedal
Copy link
Contributor

@janmarthedal janmarthedal commented Jan 1, 2021

This PR implements bit and set_bit for BigUint and BigInt.

The method names have been chosen to match those of Ramp (https://docs.rs/ramp/0.5.9/ramp/int/struct.Int.html#method.bit).

For BigInt the implementation uses the number's two's complement representation when the number is negative. This matches what libraries like Ramp or languages like Python do.

Resolves #172

Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

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

Thanks! The two's complement stuff is a bit hairy, as expected, but I don't see any problem with it.

src/biguint.rs Outdated Show resolved Hide resolved
src/biguint.rs Outdated Show resolved Hide resolved
src/biguint.rs Outdated Show resolved Hide resolved
src/bigint.rs Outdated Show resolved Hide resolved
src/bigint.rs Outdated Show resolved Hide resolved
src/bigint.rs Outdated Show resolved Hide resolved
@janmarthedal janmarthedal marked this pull request as draft January 16, 2021 15:22
@janmarthedal janmarthedal marked this pull request as ready for review January 19, 2021 13:17
@janmarthedal
Copy link
Contributor Author

janmarthedal commented Jan 19, 2021

@cuviper I have now updated the PR with a more efficient implementation of BigInt::set_bit. Basically, I have made sure that only the relevant digits will be iterated over instead of all of them, as you also pointed out. In a special benchmark it saved about 70% in runtime.

I have also implemented a special case for bit < trailing_zeros && value which saves some runtime compared to the method used in the case bit == trailing_zeros && !value, but only about 5-10%. The two cases can be merged to make set_bit a bit shorter.

@mkaczanowski
Copy link

mkaczanowski commented Feb 23, 2021

@cuviper it's been a while since the last review :) Is this PR all good? (asking, since I am waiting for this feature)

32-bit targets would fail to convert that to a `usize` digit index, but
we can reasonably say that all bits are false at that point.
@cuviper
Copy link
Member

cuviper commented Feb 24, 2021

I added a couple tweaks to the edge cases of u64 to usize, but otherwise it looks good!

bors r+

bors bot added a commit that referenced this pull request Feb 24, 2021
183: Get/set n-th bit of `BigUint` and `BigInt` r=cuviper a=janmarthedal

This PR implements `bit` and `set_bit` for `BigUint` and `BigInt`.

The method names have been chosen to match those of Ramp (https://docs.rs/ramp/0.5.9/ramp/int/struct.Int.html#method.bit).

For `BigInt` the implementation uses the number's two's complement representation when the number is negative. This matches what libraries like Ramp or languages like Python do.

Resolves #172 

Co-authored-by: Jan Marthedal Rasmussen <jan@janmr.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors
Copy link
Contributor

bors bot commented Feb 24, 2021

Build failed:

@cuviper
Copy link
Member

cuviper commented Feb 24, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 24, 2021

@bors bors bot merged commit 7492eec into rust-num:master Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How could I get/set n-th bit of BigInt?
3 participants