How do I determine the unsigned interpretation of a negative FixNum?
# unexpected, true
(~0b01111011).to_s(2) == ("-" + (~0b01111011).abs.to_s(2))
# expected, false
~0b01111011 == 0b10000100
How would I write a function such that:
123.unsigned_not(8) == 132
Or alternatively:
-124.unsigned(8) == 132
Edit: I could do this via strings, but the solution is far from satisfying
class Fixnum
def unsigned_not(bits=16)
to_s(2).rjust(bits,'0').gsub(/[01]/, '0' => '1', '1' => '0').to_i(2)
end
end
Fixnum#~operator does Two's complement and Ruby uses internally arbitrary big numbers & arithmetic, so if you want to do an inversion on a fixed base, you need to work in required bounds and interpret results accordingly: