| 1 |
# Boolean Arithmetic |
| 2 |
|
| 3 |
This merges another bit-slice into `self` with a Boolean arithmetic operation. |
| 4 |
If the other bit-slice is shorter than `self`, it is zero-extended. For `BitAnd`, |
| 5 |
this clears all excess bits of `self` to `0`; for `BitOr` and `BitXor`, it |
| 6 |
leaves them untouched |
| 7 |
|
| 8 |
## Behavior |
| 9 |
|
| 10 |
The Boolean operation proceeds across each bit-slice in iteration order. This is |
| 11 |
`3O(n)` in the length of the shorter of `self` and `rhs`. However, it can be |
| 12 |
accelerated if `rhs` has the same type parameters as `self`, and both are using |
| 13 |
one of the orderings provided by `bitvec`. In this case, the implementation |
| 14 |
specializes to use `BitField` batch operations to operate on the slices one word |
| 15 |
at a time, rather than one bit. |
| 16 |
|
| 17 |
Acceleration is not currently provided for custom bit-orderings that use the |
| 18 |
same storage type. |
| 19 |
|
| 20 |
## Pre-`1.0` Behavior |
| 21 |
|
| 22 |
In the `0.` development series, Boolean arithmetic was implemented against all |
| 23 |
`I: Iterator<Item = bool>`. This allowed code such as `bits |= [false, true];`, |
| 24 |
but forbad acceleration in the most common use case (combining two bit-slices) |
| 25 |
because `BitSlice` is not such an iterator. |
| 26 |
|
| 27 |
Usage surveys indicate that it is better for the arithmetic operators to operate |
| 28 |
on bit-slices, and to allow the possibility of specialized acceleration, rather |
| 29 |
than to allow folding against any iterator of `bool`s. |
| 30 |
|
| 31 |
If pre-`1.0` code relies on this behavior specifically, and has non-`BitSlice` |
| 32 |
arguments to the Boolean sigils, then they will need to be replaced with the |
| 33 |
equivalent loop. |
| 34 |
|
| 35 |
## Examples |
| 36 |
|
| 37 |
```rust |
| 38 |
use bitvec::prelude::*; |
| 39 |
|
| 40 |
let a = bits![mut 0, 0, 1, 1]; |
| 41 |
let b = bits![ 0, 1, 0, 1]; |
| 42 |
|
| 43 |
*a ^= b; |
| 44 |
assert_eq!(a, bits![0, 1, 1, 0]); |
| 45 |
|
| 46 |
let c = bits![mut 0, 0, 1, 1]; |
| 47 |
let d = [false, true, false, true]; |
| 48 |
|
| 49 |
// no longer allowed |
| 50 |
// c &= d.into_iter().by_vals(); |
| 51 |
for (mut c, d) in c.iter_mut().zip(d.into_iter()) |
| 52 |
{ |
| 53 |
*c ^= d; |
| 54 |
} |
| 55 |
assert_eq!(c, bits![0, 1, 1, 0]); |
| 56 |
``` |