Thank you for your help.
[Used language]
Python 3
[What do you want to do?]
Now
in the Byte (8-bit) length array (list) variable a[32]
If you consider the entire 32 Byte to be a 256-bit column,
I'd like to have this whole bit string right shift.
How can I do this?
in Python or numpy or scipy
Is there a function that can be converted once?
Thank you for your cooperation.
python
The following is an example of a 5-bit right shift.After converting the byte string to a 256-bit length integer value and shifting it back to an integer value in 8-bit increments.
>>import re
>>>lst=list(iter(b'@12Hello World, Good bye World.'))
>>>lst
[64, 49, 50, ..., 108, 100, 46]
>>len(lst)
32
>> list (map(lambdax:int(x,2),
re.findall('.{8},
format(sum([int(x)*(2**(8*(len(lst)-i-1))))for i, x in enumerate(lst)])>5, '0256b')
)))
[2, 1, 137, ..., 147, 99, 33]
This may be difficult to understand, so I will try to shift right, for example, 32-bit (4 bytes).
>>shifted=list(map(lambdax:int(x,2),
re.findall('.{8},
format(sum([int(x)*(2**(8*(len(lst)-i-1))))for i, x in enumerate(lst)))>>32, '0256b')
)))
>> str('.join(map(chr,shifted)))
'\x00\x00\x00\x00 @12 Hello World, Goodbye Wo'
I tried to implement it from the perspective that 32 int8s are collected and 256bit ints.
The example is only 32 bits long, but can be extended to any length
#-*-coding:utf-8-*-
import numpy as np
DIGITS = 8
defprint_bits(bits):
For bit in bits:
print np.binary_repr(bit).zfill(DIGITS)
# shift to the right to get the vanishing bit (left-hand side)
def get_underflow_bits(bits,n):
return np.bitwise_and (2**DIGITS-1, np.left_shift (bits, DIGITS-n))
# Returns only bits moved to the next array as a result of the right shift
def get_underflows(bits,n):
return np.insert(get_underflow_bits(bits,n),0,0)[:-1]
# Take OR of simple right-shift results and shift
default_shift(bits,n):
return np.bitwise_or(np.right_shift(bits,n), get_underflows(bits,n))
sample = [255,0,255,0]
print_bits(right_shift(sample,3))
This is an implementation that creates a large integer once and then shifts it to the right to return it to the array.For your information.
from functools import redundancy
default(a, bits):
D = 8
n = len(a)
value=reduce(lambdax, y:x|y, (e<<(D*(n-1-i))for i, e in enumerate(a))))
value>>=bits
s = '{:0 {}b}'.format(value, len(a)*D)
return [ int[i*D:(i+1)*D], 2) for i in range(n)]
a = [0b11111111, 0b010101, 0b00001111]
print(''.join('{:08b}'.format(x) for x in shift(a,0)))#111111101010100001111
print(''.join('{:08b}'.format(x) for x in shift(a,1)))#011111110101010000111
print(''.join('{:08b}'.format(x) for x in shift(a,2)))#0011111111010101000011
print(''.join('{:08b}'.format(x) for x in shift(a,3)))#00011111111010101010100001
print(''.join('{:08b}'.format(x) for x in shift(a,4)))#000011111111111101010101010000
print(''.join('{:08b}'.format(x) for x in shift(a,5)))#000001111111010101000
print(''.join('{:08b}'.format(x) for x in shift(a,6)))#00000011111111110101010100
print(''.join('{:08b}'.format(x) for x in shift(a,7)))#00000001 1111111010101010
print(''.join('{:08b}'.format(x) for x in shift(a,8)))#000000001111111110101
print(''.join('{:08b}'.format(x) for x in shift(a,9)))#00000000111111101010
print(''.join('{:08b}'.format(x) for x in shift(a,10)))#000000000011111111010101
© 2023 OneMinuteCode. All rights reserved.