Skip to content

Commit

Permalink
set_bit(), test_bit(), and company: support negatives (#54)
Browse files Browse the repository at this point in the history
This is a breaking change.

Currently, `test_bit()`, `set_bit()`, `clear_bit()`, and `invert_bit()`
operate on the magnitude of an `Arbi` integer. With this commit, two's
complement representation is assumed for negative integers.
  • Loading branch information
OTheDev authored Jan 15, 2025
1 parent 6e73841 commit 606c578
Show file tree
Hide file tree
Showing 2 changed files with 632 additions and 168 deletions.
7 changes: 4 additions & 3 deletions arbi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,20 +254,21 @@ extension).
- Bitwise complement: `!`.
- Bitwise AND, OR, and XOR: `&`, `|`, `^`, `&=`, `|=`, `^=`.

Test or set a bit at a specified index (zero-based) on the absolute value of an
Test, set, clear, or invert (i.e. toggle) a bit at a specified index on the
two's complement representation (with sign extension) of an
[`Arbi`](https://docs.rs/arbi/latest/arbi/struct.Arbi.html) integer:
- [`Arbi::test_bit()`](https://docs.rs/arbi/latest/arbi/struct.Arbi.html#method.test_bit)
- [`Arbi::set_bit()`](https://docs.rs/arbi/latest/arbi/struct.Arbi.html#method.set_bit)
- [`Arbi::clear_bit()`](https://docs.rs/arbi/latest/arbi/struct.Arbi.html#method.clear_bit)
- [`Arbi::invert_bit()`](https://docs.rs/arbi/latest/arbi/struct.Arbi.html#method.invert_bit)

Obtain the number of bits needed to represent the absolute value of an [`Arbi`](https://docs.rs/arbi/latest/arbi/struct.Arbi.html)
integer using [`Arbi::size_bits()`](https://docs.rs/arbi/latest/arbi/struct.Arbi.html#method.size_bits):

```rust
use arbi::Arbi;

let mut a = Arbi::zero();
assert_eq!(a.size_bits(), 0);

a.incr(); // 1
assert_eq!(a.size_bits(), 1);
a.incr(); // 10
Expand Down
Loading

0 comments on commit 606c578

Please sign in to comment.