Hi,
I would like to understand what does the following "<<" does in the following statement?

 (ord(msg[i+1]) << 8)

As it is two less than sign, I couldn't find some meaningful response on google:(

thanks.

Recommended Answers

All 2 Replies

If n is an integer, n << 8 is equivalent to n * (2**8). The << operator is called the left shift operator. It comes from the binary representation of integers, for example 6 is represented as 110 in binary form, and 6 << 3 as 110000. Other languages use the same convention (in particular the C language, the mother of so many languages).

In python there is no theoretical limit to the size of integers, so that you can write 6 << 1000 and still obtain 6 * 2 ** 1000. This wouldn't work in languages where integers are limited to 32 bits or 64 bits.

Another feature of python is that the binary operator << can be overloaded and have an arbitrary different meaning for user defined data types.

Thank you for the detailed explanation Gribouillis.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.