DEV Community

Discussion on: Daily Challenge #255 - Is There an Odd Bit?

Collapse
 
habisain profile image
Aleph Fell

Simple Python solution, exploiting that any_even has a nice recursive case.

def any_even(x):
   if x == 0: return False
   elif x % 2: return True
   else: return any_even(x >> 2)
def any_odd(x): return any_even(x >> 1)

Note that if you know the size of an integer you could do this with bitmasking, so languages like C have much more efficient (and somewhat less interesting) solutions. Languages with unbounded ints (like Python) obviously can't do this.