https://github.com/mingrammer/bit-counter
:bulb: A simple bit counter
https://github.com/mingrammer/bit-counter
bit counter
Last synced: 12 months ago
JSON representation
:bulb: A simple bit counter
- Host: GitHub
- URL: https://github.com/mingrammer/bit-counter
- Owner: mingrammer
- License: mit
- Created: 2018-03-26T14:53:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-26T16:09:39.000Z (over 8 years ago)
- Last Synced: 2025-06-02T01:15:44.084Z (about 1 year ago)
- Topics: bit, counter
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Bit Counter
A simple bit counter. There are 2 types of bit counter: increment and decrement. Bit counter idea was borrowed from digital bit counter. Each index of an array represents each bit of a bit sequence.
## Run & Test
```bash
# 6 bit counter example
python3 main.py
# 8 bit counter tests
python3 test.py
```
## Increment
Increment the bit count by 1. It inverts 1 to 0 until it encounters 0, and inverts 0 to 1 when it encounters 0.
```
00000000
00000001
00000010
00000011
00000100
........
```
### How it works
```
000111
^ (1 to 0)
000110
^ (1 to 0)
000100
^ (1 to 0)
001000
^ (0 to 1)
```
### Implementation
```python
def increment(bits):
i = 0
m = len(bits) - 1
while i <= m and bits[m-i] == 1:
bits[m-i] = 0
i += 1
# Index overflow prevention
if i <= m:
bits[m-i] = 1
```
## Decrement
Decrement the bit count by 1. It inverts 0 to 1 until it encounters 1, and inverts 1 to 0 when it encounters 1.
```
11111111
11111110
11111101
11111100
11111011
........
```
### How it works
```
001000
^ (0 to 1)
001001
^ (0 to 1)
001011
^ (0 to 1)
000111
^ (1 to 0)
```
### Implementation
```python
def decrement(bits):
i = 0
m = len(bits) - 1
while i <= m and bits[m-i] == 0:
bits[m-i] = 1
i += 1
# Index overflow prevention
if i <= m:
bits[m-i] = 0
```