Bit Manipulation - Floating Decimal to Binary
def print_binary ( n )
if n >= 1 or n <= 0
return "ERROR"
end
res = '.'
while n > 0
p n
p res
if res . length > 32
return 'ERROR'
end
r = n * 2
if r >= 1
res << '1'
n = r - 1
else
res << '0'
n = r
end
end
return res
end
def print_frac_binary ( n )
if n >= 1 or n <= 0
return "ERROR"
end
res = '.'
frac = 0 . 5
while n > 0
p n
p res
if res . length > 32
return 'ERROR'
end
if n >= frac
res << '1'
n = n - frac
else
res << '0'
end
frac /= 2
end
return res
end
p print_binary ( 0 . 78 )
p "---------------------"
p print_frac_binary ( 0 . 78 )
# Output:
# karthiks-MacBook-Pro-2:code karthikselvakumarbhuvaneswaran$ ruby decimal_in_binary.rb
# 0.78
# "."
# 0.56
# ".1"
# 0.1200000000000001
# ".11"
# 0.2400000000000002
# ".110"
# 0.4800000000000004
# ".1100"
# 0.9600000000000009
# ".11000"
# 0.9200000000000017
# ".110001"
# 0.8400000000000034
# ".1100011"
# 0.6800000000000068
# ".11000111"
# 0.36000000000001364
# ".110001111"
# 0.7200000000000273
# ".1100011110"
# 0.44000000000005457
# ".11000111101"
# 0.8800000000001091
# ".110001111010"
# 0.7600000000002183
# ".1100011110101"
# 0.5200000000004366
# ".11000111101011"
# 0.040000000000873115
# ".110001111010111"
# 0.08000000000174623
# ".1100011110101110"
# 0.16000000000349246
# ".11000111101011100"
# 0.3200000000069849
# ".110001111010111000"
# 0.6400000000139698
# ".1100011110101110000"
# 0.2800000000279397
# ".11000111101011100001"
# 0.5600000000558794
# ".110001111010111000010"
# 0.12000000011175871
# ".1100011110101110000101"
# 0.24000000022351742
# ".11000111101011100001010"
# 0.48000000044703484
# ".110001111010111000010100"
# 0.9600000008940697
# ".1100011110101110000101000"
# 0.9200000017881393
# ".11000111101011100001010001"
# 0.8400000035762787
# ".110001111010111000010100011"
# 0.6800000071525574
# ".1100011110101110000101000111"
# 0.36000001430511475
# ".11000111101011100001010001111"
# 0.7200000286102295
# ".110001111010111000010100011110"
# 0.440000057220459
# ".1100011110101110000101000111101"
# 0.880000114440918
# ".11000111101011100001010001111010"
# "ERROR"
# "---------------------"
# 0.78
# "."
# 0.28
# ".1"
# 0.030000000000000027
# ".11"
# 0.030000000000000027
# ".110"
# 0.030000000000000027
# ".1100"
# 0.030000000000000027
# ".11000"
# 0.014375000000000027
# ".110001"
# 0.006562500000000027
# ".1100011"
# 0.0026562500000000266
# ".11000111"
# 0.0007031250000000266
# ".110001111"
# 0.0007031250000000266
# ".1100011110"
# 0.00021484375000002665
# ".11000111101"
# 0.00021484375000002665
# ".110001111010"
# 9.277343750002665e-05
# ".1100011110101"
# 3.1738281250026645e-05
# ".11000111101011"
# 1.2207031250266454e-06
# ".110001111010111"
# 1.2207031250266454e-06
# ".1100011110101110"
# 1.2207031250266454e-06
# ".11000111101011100"
# 1.2207031250266454e-06
# ".110001111010111000"
# 1.2207031250266454e-06
# ".1100011110101110000"
# 2.6702880862039535e-07
# ".11000111101011100001"
# 2.6702880862039535e-07
# ".110001111010111000010"
# 2.8610229518832853e-08
# ".1100011110101110000101"
# 2.8610229518832853e-08
# ".11000111101011100001010"
# 2.8610229518832853e-08
# ".110001111010111000010100"
# 2.8610229518832853e-08
# ".1100011110101110000101000"
# 1.3709068324985196e-08
# ".11000111101011100001010001"
# 6.258487728061368e-09
# ".110001111010111000010100011"
# 2.533197429599454e-09
# ".1100011110101110000101000111"
# 6.705522803684971e-10
# ".11000111101011100001010001111"
# 6.705522803684971e-10
# ".110001111010111000010100011110"
# 2.0489099306075786e-10
# ".1100011110101110000101000111101"
# 2.0489099306075786e-10
# ".11000111101011100001010001111010"
# "ERROR"