Utilize Python bit operators

Asked 2 years ago, Updated 2 years ago, 16 views

study libraryA

    while i != 0:
        result.append(i & 0xFF)
        i >>= 8

I found this part.

In Python, >> is a bit operator that shifts to the right.

But the bit operator I know is

100 >> 2 = Shift 2 spaces to the right

100 <<4 = shift by 4 spaces to the left

I understand that you are using it, but I don't know what the code is for.

SyntaxError occurs if you just enter the command.

3234234 >>=8
>>> SyntaxError: can't assign to literal

I copied the library exactly the same and printed it out.

i = 1241535

while i!=0:
     result.append(i&0xFF)
     print(i)
     i>>=8
     print(i)
     print('-------------')
1241535
4849
-----------
4849
18
----------
18
0
---------

I think it's causing a shift if you look at the decreasing number

I wonder exactly what that >>= does.

If you have any explanations or related materials, please advise.

Thank you.

python

2022-09-21 22:04

1 Answers

i >>= 8 means shift the i value by 8 bits and put it in i.

I used it like i + = 1.

3234234 >>=8 caused a syntax error because the constant cannot be substituted.


2022-09-21 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.