Skip to content

Commit

Permalink
Merge branch 'main' of github.com:danlehmann/arbitrary-int
Browse files Browse the repository at this point in the history
  • Loading branch information
danlehmann committed Aug 23, 2022
2 parents 4353623 + 19c1cc6 commit 3874fb0
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ In this example, a will have 5 bytes and be represented by a u8. This is identic
## Extract
A common source for arbitrary integers is by extracting them from bitfields. For example, if data contained 32 bits and we want to extract bits 4..=9, we could perform the following:

`let a = u6::new((data >> 4) & 0b111111);`
`let a = u6::new(((data >> 4) & 0b111111) as u8);`

This is a pretty common operation, but it's easy to get it wrong: The number of 1s and u6 have to match. Also, new() will internally perform a bounds-check, which can panic.
This is a pretty common operation, but it's easy to get it wrong: The number of 1s and u6 have to match. Also, new() will internally perform a bounds-check, which can panic. Thirdly, a type-cast is often needed.
To make this easier, various extract methods exist that handle shifting and masking, for example:

```
Expand Down

0 comments on commit 3874fb0

Please sign in to comment.