Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
|||
from operator import or_
|
||||
from functools import reduce
|
||||
|
||||
def set_right_adjacent_bits(n: int, b: int) -> int:
|
||||
return reduce(or_, (b >> x for x in range(n+1)), 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("SAME n & Width.\n")
|
||||
n = 2 # bits to the right of set bits, to also set
|
||||
bits = "1000 0100 0010 0000"
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = int(b_str, 2)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits(n, b)
|
||||
print(f" Input b: {b:0{e}b}")
|
||||
print(f" Result: {result:0{e}b}\n")
|
||||
|
||||
print("SAME Input & Width.\n")
|
||||
#bits = "01000010001001010110"
|
||||
bits = '01' + '1'.join('0'*x for x in range(10, 0, -1))
|
||||
for n in range(4):
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = int(b_str, 2)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits(n, b)
|
||||
print(f" Input b: {b:0{e}b}")
|
||||
print(f" Result: {result:0{e}b}\n")
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
from typing import List
|
||||
|
||||
|
||||
def set_right_adjacent_bits_list(n: int, b: List[int]) -> List[int]:
|
||||
# [0]*x is padding b on the left.
|
||||
# zip(*(list1, list2,..)) returns the n'th elements on list1, list2,...
|
||||
# int(any(...)) or's them.
|
||||
return [int(any(shifts))
|
||||
for shifts in zip(*([0]*x + b for x in range(n+1)))]
|
||||
|
||||
def _list2bin(b: List[int]) -> str:
|
||||
"List of 0/1 ints to bool string."
|
||||
return ''.join(str(x) for x in b)
|
||||
|
||||
def _to_list(bits: str) -> List[int]:
|
||||
return [int(char) for char in bits]
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("SAME n & Width.\n")
|
||||
n = 2 # bits to the right of set bits, to also set
|
||||
bits = "1000 0100 0010 0000"
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = _to_list(b_str)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits_list(n, b)
|
||||
print(f" Input b: {_list2bin(b)}")
|
||||
print(f" Result: {_list2bin(result)}\n")
|
||||
|
||||
print("SAME Input & Width.\n")
|
||||
#bits = "01000010001001010110"
|
||||
bits = '01' + '1'.join('0'*x for x in range(10, 0, -1))
|
||||
for n in range(4):
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = _to_list(b_str)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits_list(n, b)
|
||||
print(f" Input b: {_list2bin(b)}")
|
||||
print(f" Result: {_list2bin(result)}\n")
|
||||
Loading…
Add table
Add a link
Reference in a new issue