, ~, Fixnum: , , , 0 9 ( 1001). , Fixnum. ( )
1.size
2147483647.size
~ Bignum. , , Bignum , , 1 Bignum. , , - Bignum . , .
In order to answer this question, you put it right off the bat, you can find the greatest power 2 less than data input, double it, subtract 1, then XOR is the result of the fact that with your input you always get one complement only significant bits in your input number.
def sig_ones_complement(num)
significant_bits = num.to_s(2).length
next_smallest_pow_2 = 2**(significant_bits-1)
xor_mask = (2*next_smallest_pow_2)-1
return num ^ xor_mask
end
source
share