Here is my array:
array([-0.1142 , 0.11127 , 0.0374 , 0.02007 , -0.05737 , -0.02058 ,
-0.1595 , -0.1288 , 0.1436 , -0.05212 , 0.2437 , 0.0046 ,
-0.1456 , -0.09485 , -0.0788 , 0.1755 , -0.2429 , -0.1204 ,
-0.01064 , 0.01154 , 0.06058 , -0.02666 , 0.01773 , 0.03436 ,
-0.1262 , -0.3428 , -0.068 , -0.10645 , 0.0669 , -0.02094 ,
-0.0751 , 0.001348, -0.1737 , 0.01146 , 0.01648 , 0.03613 ,
0.03384 , -0.063 , 0.1617 , -0.03023 , -0.258 , 0.0385 ,
0.0382 , 0.1821 , 0.2104 , -0.01604 , 0.05945 , -0.1809 ,
0.1847 , -0.1569 , 0.02007 , 0.1757 , 0.08514 , 0.07886 ,
-0.00872 , -0.1108 , -0.01473 , 0.1075 , -0.1221 , 0.0163 ,
0.03275 , -0.01775 , 0.01232 , -0.0705 ], dtype=float16)
I need to convert it into binary that should recognize the decimal and negative sign then every element should output 16-bit but I don't know how please help me. I am using Jupyter Notebook to run my python program.
Use the struct
module to interpret the half (16-bit float) as an integer, then convert that integer to a 16-character long binary string.
Per thedocumentation , !
means use the computer's system (either big-endian or little-endian) for the ordering of the two bytes, H
means a unsigned short
(16-bit unsigned integer), and e
means a half
(16-bit floating point number)
import numpy as np
import struct
arr = np.array([-0.1142 , 0.11127 , 0.0374 , 0.02007 , -0.05737 , -0.02058 ,
-0.1595 , -0.1288 , 0.1436 , -0.05212 , 0.2437 , 0.0046 ,
-0.1456 , -0.09485 , -0.0788 , 0.1755 , -0.2429 , -0.1204 ,
-0.01064 , 0.01154 , 0.06058 , -0.02666 , 0.01773 , 0.03436 ,
-0.1262 , -0.3428 , -0.068 , -0.10645 , 0.0669 , -0.02094 ,
-0.0751 , 0.001348, -0.1737 , 0.01146 , 0.01648 , 0.03613 ,
0.03384 , -0.063 , 0.1617 , -0.03023 , -0.258 , 0.0385 ,
0.0382 , 0.1821 , 0.2104 , -0.01604 , 0.05945 , -0.1809 ,
0.1847 , -0.1569 , 0.02007 , 0.1757 , 0.08514 , 0.07886 ,
-0.00872 , -0.1108 , -0.01473 , 0.1075 , -0.1221 , 0.0163 ,
0.03275 , -0.01775 , 0.01232 , -0.0705 ], dtype=np.float16)
def half_to_binstr(half):
bits, = struct.unpack('!H', struct.pack('!e', half))
return "{:016b}".format(bits)
res = np.vectorize(half_to_binstr)(arr)
Output:
array(['1010111101001111', '0010111100011111', '0010100011001010',
'0010010100100011', '1010101101011000', '1010010101000101',
'1011000100011011', '1011000000011111', '0011000010011000',
'1010101010101100', '0011001111001100', '0001110010110110',
'1011000010101001', '1010111000010010', '1010110100001011',
'0011000110011110', '1011001111000110', '1010111110110101',
'1010000101110011', '0010000111101001', '0010101111000001',
'1010011011010011', '0010010010001010', '0010100001100110',
'1011000000001010', '1011010101111100', '1010110001011010',
'1010111011010000', '0010110001001000', '1010010101011100',
'1010110011001110', '0001010110000101', '1011000110001111',
'0010000111011110', '0010010000111000', '0010100010100000',
'0010100001010101', '1010110000001000', '0011000100101101',
'1010011110111101', '1011010000100001', '0010100011101110',
'0010100011100100', '0011000111010100', '0011001010111100',
'1010010000011011', '0010101110011100', '1011000111001010',
'0011000111101001', '1011000100000101', '0010010100100011',
'0011000110011111', '0010110101110011', '0010110100001100',
'1010000001110111', '1010111100010111', '1010001110001011',
'0010111011100001', '1010111111010000', '0010010000101100',
'0010100000110001', '1010010010001011', '0010001001001111',
'1010110010000011'], dtype='<U16')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.