0

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.

2
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by tirexxerit
3

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.