Verilog floating point for binary conversion

I am trying to convert a signed floating-point number in Verilog to a signed 24-bit binary value. for instance

-0.0065 would become: 24'b100000000110101001111110
0.0901 would become: 24'b000001011100010000110010

It is right? Thanks you

0
source share
1 answer

Taking a decimal fraction 0.0901and converting to a fixed-point value with 2 integer bits and 22 fractional bits (24 bits in total):

Ruby syntax used for math (you can cut and paste the command line tool into an IRB (interactive ruby)):

i = (0.0901 * 2**20) # 377906.7904
# truncat to integer value
i = i.to_i # 377906

# Convert to string using binary (base 2)
i.to_s(2) # "1011100010000110010"

To add leading zeros (24-bit length), right alignment and pad with 0

i.to_s(2).rjust(24, '0') # "000001011100010000110010"

# Convert to Hex (base 16)
i.to_s(16) # "5c432"

, , :

(0.0065 * 2**22).to_i.to_s(2).rjust(24, '0')
=> "000000000110101001111110"

"000000000110101001111110"
"111111111001010110000001" # Ones complement (bit invert)
"111111111001010110000001" + 1 
"111111111001010110000010" #Twos complement

24'b100000000110101001111110, MSB, 1, . , , - Sign Magnitude, ( ).

NB: . 377906.7904. , 0.7904, .

0

All Articles