I would like to know why the following expressions produce the same result:
# method 1
def produce(n)
n % 16
end
#method 2
def produce(n)
n & 15
end
I understand that method 1 uses modulo operation, and that the method 2 uses 'bitwise AND'. But I am a little lost as to why calling & with 15 is the same as calling modulo 16.
I have a hunch that it only works for modulo x where x is 2^n. Any ideas?