Skip to content

Commit

Permalink
Added power2 function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ballenspectric committed Jul 16, 2024
1 parent 1fe40ba commit a6869cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ e = j1950_to_epoch(2191001400.0)
j = epoch_to_to_j1950(1559849400.0)
```

## power2

Calculates the next power of two greater than or equal to x.

```python
power2(3) # returns 4
```

## bluefile

xmextras locates the system installation of bluefile.py so it can be imported outside of an X-Midas environment.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_noxm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ def test_j1950():
assert datetime.fromtimestamp(t) == xmextras.noxm.j1950_to_datetime(
xmextras.noxm.epoch_to_j1950(t)
)

def test_power2():
assert xmextras.noxm.power2(-3) == 1
assert xmextras.noxm.power2(0) == 1
assert xmextras.noxm.power2(1) == 1
assert xmextras.noxm.power2(2) == 2
assert xmextras.noxm.power2(3) == 4
assert xmextras.noxm.power2(65537) == 131072
1 change: 1 addition & 0 deletions xmextras/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
epoch_to_j1950,
j1950_to_datetime,
j1950_to_epoch,
power2,
)

try:
Expand Down
10 changes: 10 additions & 0 deletions xmextras/noxm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ def datetime_to_j1950(dt):
Returns the J1950 time from a datetime object.
'''
return epoch_to_j1950(dt.timestamp())

def power2(x):
'''
Returns the next power of two greater than
or equal to x.
'''
if x < 1:
return 1

return 1 << (x-1).bit_length()

0 comments on commit a6869cb

Please sign in to comment.