Ad
  • Custom User Avatar

    Hmmm. Hard to say. Did some microbenchmark tests, and sometimes the bitwise operation is faster, and sometimes the old fashioned exponential computation is faster. And I simulated very many trials with a wide range of powers, so I doubt there is any benefit to using the bitwise operation.

    Other than being fun-spritied, I wouldn't really dub this a very readable code, since not many people know the bitwise operator in Python.

  • Custom User Avatar

    I actually never knew this. This is about 25% faster than the traditional method of 2 ** 5. Going to treasure this nugget of knowledge forever.

  • Custom User Avatar

    Not faster in Python. This is just C heritage.

  • Custom User Avatar

    Wow, this is really clever! For those that don't get it, this is exactly the same as 2**len(list) but is much faster because its doing bitwise operations.

    Basically, 1<<len(list) means you take the bit representing 1: 00000001 and move that one down the byte len(list) bits. so a set with length 5 would push the bit over 5 to be 00100000 == 32 == 2^5.

    Brilliant.