[Tutor] [spoiler] Re: Shifting arrays as though they are a 'word'

Chip Wachob wachobc at gmail.com
Mon Oct 8 21:48:01 EDT 2018


First,

Steven,

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
>> bytes
>>
>> 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()[0]
>>
>> # 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
the manufacturer.

>
> py> b = bytearray(4)  # work with four bytes
> py> b[1] = 255  # fill in some non-zero values
> py> b[2] = 127
> py> b
> bytearray(b'\x00\xff\x7f\x00')
> 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
handy?

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)
> '0xff7f00'
> py> bin(n >> 1)
> '0b11111111011111110000000'
> py> bin(n << 1)
> '0b1111111101111111000000000'
>
> Does that help?
>

It does help somewhat.

>
>
>
> --
> Steve
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list