如何确定负 FixNum 的无符号解释?
# unexpected, true
(~0b01111011).to_s(2) == ("-" + (~0b01111011).abs.to_s(2))
# expected, false
~0b01111011 == 0b10000100
我将如何编写这样的函数:
123.unsigned_not(8) == 132
或者:
-124.unsigned(8) == 132
编辑:我可以通过字符串来做到这一点,但解决方案远不能令人满意
class Fixnum
def unsigned_not(bits=16)
to_s(2).rjust(bits,'0').gsub(/[01]/, '0' => '1', '1' => '0').to_i(2)
end
end