programming-examples/ruby/_Basics/UnsetRightmostSetBit.rb

8 lines
283 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
#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