Skip to content
Closed

Master #14573

Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions bit_manipulation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,22 @@ Bit manipulation is the act of manipulating bits to detect errors (hamming code)
* <https://wiki.python.org/moin/BitManipulation>
* <https://wiki.python.org/moin/BitwiseOperators>
* <https://www.tutorialspoint.com/python3/bitwise_operators_example.htm>

## Example

Below is a simple example using the `get_set_bits_count_using_brian_kernighans_algorithm`
function from [bit_manipulation/count_number_of_one_bits.py](bit_manipulation/count_number_of_one_bits.py).

```python
from bit_manipulation.count_number_of_one_bits import get_set_bits_count_using_brian_kernighans_algorithm

print(get_set_bits_count_using_brian_kernighans_algorithm(25)) # 3
print(get_set_bits_count_using_brian_kernighans_algorithm(58)) # 4
```

This repository also includes doctest examples in the implementation that can be run with:

```bash
python3 bit_manipulation/count_number_of_one_bits.py
```

22 changes: 22 additions & 0 deletions tests/test_count_number_of_one_bits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest

Check failure on line 1 in tests/test_count_number_of_one_bits.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (INP001)

tests/test_count_number_of_one_bits.py:1:1: INP001 File `tests/test_count_number_of_one_bits.py` is part of an implicit namespace package. Add an `__init__.py`.

from bit_manipulation.count_number_of_one_bits import (
get_set_bits_count_using_brian_kernighans_algorithm,
get_set_bits_count_using_modulo_operator,
)


class TestCountSetBits(unittest.TestCase):
def test_examples(self):
self.assertEqual(get_set_bits_count_using_brian_kernighans_algorithm(25), 3)

Check failure on line 11 in tests/test_count_number_of_one_bits.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (PT009)

tests/test_count_number_of_one_bits.py:11:9: PT009 Use a regular `assert` instead of unittest-style `assertEqual` help: Replace `assertEqual(...)` with `assert ...`
self.assertEqual(get_set_bits_count_using_brian_kernighans_algorithm(58), 4)

Check failure on line 12 in tests/test_count_number_of_one_bits.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (PT009)

tests/test_count_number_of_one_bits.py:12:9: PT009 Use a regular `assert` instead of unittest-style `assertEqual` help: Replace `assertEqual(...)` with `assert ...`
self.assertEqual(get_set_bits_count_using_modulo_operator(37), 3)

Check failure on line 13 in tests/test_count_number_of_one_bits.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (PT009)

tests/test_count_number_of_one_bits.py:13:9: PT009 Use a regular `assert` instead of unittest-style `assertEqual` help: Replace `assertEqual(...)` with `assert ...`
self.assertEqual(get_set_bits_count_using_modulo_operator(0), 0)

Check failure on line 14 in tests/test_count_number_of_one_bits.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (PT009)

tests/test_count_number_of_one_bits.py:14:9: PT009 Use a regular `assert` instead of unittest-style `assertEqual` help: Replace `assertEqual(...)` with `assert ...`

def test_negative_input_raises(self):
with self.assertRaises(ValueError):

Check failure on line 17 in tests/test_count_number_of_one_bits.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (PT027)

tests/test_count_number_of_one_bits.py:17:14: PT027 Use `pytest.raises` instead of unittest-style `assertRaises` help: Replace `assertRaises` with `pytest.raises`
get_set_bits_count_using_brian_kernighans_algorithm(-1)


if __name__ == "__main__":
unittest.main()
Loading