You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8 lines
283 B
Ruby

#Given a binary number unset the rightmost set bit.
#Algorithm: The biwise "&" of any number n with n-1 unsets the rightmost set bit
#e.g. 5=101 ,5-1=4=100 ,5&4=(101)&(100)=100(result with rightmost bit unset)
def unset_rightmost(n)
return n&(n-1)
end
unset_rightmost(3) # => 2