I want Python to bit shift the entire 32 one-dimensional arrays.

Asked 5 months ago, Updated 5 months ago, 12 views

Thank you for your help.

[Used language]

Python 3

[What do you want to do?]

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.


2022-09-30 14:14

3 Answers

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.'))
[64, 49, 50, ..., 108, 100, 46]

>> list (map(lambdax:int(x,2),
       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).

       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'

2022-09-30 14:14

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


import numpy as np


    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
    return np.bitwise_or(np.right_shift(bits,n), get_underflows(bits,n))

sample = [255,0,255,0]

2022-09-30 14:14

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))))
    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

2022-09-30 14:14

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.