What does << do in Python? -
i solved problem on project euler took 4 minutes run, above recommended time, looking through different solutions in forum. 1 of them included symbol <<
in list comprehension. looked like
blist.extend([(i << 1) + 3 in range(num) if alist.get(i)])
i can't find anywhere <<
symbol does. can me?
it's bit shift operator (python docs), , common among many programming languages, such c, java, php, etc. according python docs:
they shift first argument left or right number of bits given second argument.
a right shift n bits defined division pow(2, n). left shift n bits defined multiplication pow(2, n). negative shift counts raise
valueerror
exception.
so in specific case, i << 1
means left shift 1 bit, equivalent multiplying 2^1, or 2.
Comments
Post a Comment