[Tutor] [spoiler] Re: Shifting arrays as though they are a 'word'
wachobc at gmail.com
Mon Oct 8 21:48:01 EDT 2018
Thank you for your insight.
On 10/8/18, Steven D'Aprano <steve at pearwood.info> wrote:
> On Mon, Oct 08, 2018 at 09:16:16AM -0400, Chip Wachob wrote:
>> - What follows is a mini version, the array could be any size up to 64
>> input: 10010010 . 11101111 . 01010011
>> shift 'x' (for example, I'll use 1)
>> output: 01001001 . 01110111 . 10101001
> The first two seem to be simple right bit-shift:
> 10010010 -> 01001001
> 11101111 -> 01110111
> but the third seems to be something completely different, neither a left
> nor a right shift:
> 01010011 -> 10101001
I _think_ that you got the input and output confused on the left-most
byte. I checked to make sure that I had not made a typo, but I think
that it should be
10101001 => 01010011 which is a right shift of one bit.
> A left-bit shift would give this: 10100110
> and a right shift would give this: 00101001
> So now I don't know what you want.
>> The above are not actual data that have been manipulated by the
>> software but illustrations of observations. The real data is 'random'
>> so I'm doing the best to recall from memory what happens.
> Seriously, "this is what I vaguely remember happening" is no way to
> debug software. Show us *actual data* with *actual results* and maybe we
> can help, otherwise we're just pissing into the wind here.
I want to say also that I realize that my ethereal results were not
very helpful. I'm having trouble actually capturing real data as it
seems to be 'intermittent'. I fully expected the Brain Trust to come
back with A) There's a much better way to do what you're doing, or B)
Hey, there's a function for that, etc.
I'm going to keep digging into my problem using these suggestions and
get to the bottom of it.
I'm sorry if you felt that I was wasting your time.
>> Ideally, I'd love to be able to say :
>> # here's the array / list
>> rx_data = 
>> # read through all the bytes
>> # stash the results into the list
>> for x in range (bytes_of_data):
>> rx_data[x] = read_data()
>> # bit shift the entire lot
>> rx_data = rx_data >> bits_to_shift.
>> Which would provide me with the output described above.
> Take your byte-array returned from read_data, the *lot* of it, not just
> the first byte. Convert to an int, and shift the int.
I would be happy to take the 'lot' of it, except that the function
returns only one byte at a time. It's a library / driver provided by
> py> b = bytearray(4) # work with four bytes
> py> b = 255 # fill in some non-zero values
> py> b = 127
> py> b
> py> n = int.from_bytes(b, 'big')
I'm not familiar with this int.from_bytes.
Being new to Python, I don't have a mental library of these nifty
functions. How / where does one find a library of these wonderful
functions so I can search to find something that might be really
Could I use the int.from_bytes on a 64-byte array, or would that end
up breaking things? Then, I could shift the whole mess, but I would
have to break it back apart to use the data. Which, I presume I could
slice out of the larger 'word'.
> py> hex(n)
> py> bin(n >> 1)
> py> bin(n << 1)
> Does that help?
It does help somewhat.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
More information about the Tutor