Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the `<<` operator equivalent to `pow(2,n)` in python 3?

In the python documentation for python 3.5 https://docs.python.org/3/library/stdtypes.html

It is said that a left shift bitwise operator int << n is equivalent to the expression int * pow(2,n) but without overflow checking.

But since python automatically promotes integers to longs of arbitrary precision, does this mean that there is literally no difference (since there cant technically ever be an overflow)?

like image 359
AlanSTACK Avatar asked Dec 09 '25 05:12

AlanSTACK


1 Answers

You will not have to worry about overflows in Python unless you are using operations that involve data types that can be overflowed. This does not include Python's built in number types (int, float, long, complex).

For positive values of n, the documentation holds. The reason they specify that overflow checking is done is because external libraries, especially those implemented in C, are subject to overflow if such checking is not being done.

As mentioned in the documentation for pow, this is also equivalent to int * (2**n).

like image 199
Jamie Counsell Avatar answered Dec 11 '25 10:12

Jamie Counsell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!