Skip to content

Added implementation of "Compute parity in parallel" #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,25 @@ Note that 0 is incorrectly considered a power of 2 here. To remedy this, use:
### Compute parity by lookup table
### Compute parity of a byte using 64-bit multiply and modulus division
### Compute parity of word with a multiply

### Compute parity in parallel

```python
>>> v ^= v >> 16
>>> v ^= v >> 8
>>> v ^= v >> 4
>>> v &= 0xf
>>> even_parity = (0x6996 >> v) & 1
```

The method above takes around 9 operations, and works for 32-bit words. It may be optimized to work just on bytes in 5 operations by removing the first two lines.

The method first shifts and XORs the eight nibbles of the 32-bit value together, leaving the result in the lowest nibble of v. Next, the binary number `0110 1001 1001 0110` (`0x6996` in hex) is shifted to the right by the value represented in the lowest nibble of v. This number is like a miniature 16-bit parity-table indexed by the low four bits in v. The result has the parity of `v` in bit 1, which is masked and returned.

Thanks to Mathew Hendry for pointing out the shift-lookup idea at the end on Dec. 15, 2002. That optimization shaves two operations off using only shifting and XORing to find the parity.

> If you want "odd parity", you can change the last line to `odd_parity = 1 - (0x6996 >> v) & 1`

## Swapping Values
### Swapping values with subtraction and addition
### Swapping values with XOR
Expand Down Expand Up @@ -288,4 +306,4 @@ Please take a minute to go over the [Contribution Guidelines](CONTRIBUTING.md).

Contributions are welcome!
* [Issue reports](https://github.com/ianbrayoni/bithacks/issues)
* [Pull Requests](https://github.com/ianbrayoni/bithacks/pulls)
* [Pull Requests](https://github.com/ianbrayoni/bithacks/pulls)