代码拉取完成,页面将自动刷新
from ctypes import string_at
from sys import getsizeof
from binascii import hexlify
# def decode_num(self, encode_num):
# decoded_value = encode_num.decode()
# return decoded_value
#
#
# def encode_num(self, number):
# # rnd = random.SystemRandom().randrange(1, self.max_int)
# encoded_value = FixedPointNumber.encode(number, self.n, self.max_int, precision=None)
# return encoded_value
def encoding(number, bit_lenth=32):
# type(number) = int
if isinstance(number, int):
int_bit_list = [0 for x in range(bit_lenth)]
abs_number = abs(number)
for i in range(bit_lenth):
bit = abs_number & 1
abs_number = abs_number >> 1
int_bit_list[i] = bit
int_bit_list.reverse()
if number < 0:
int_bit_list[0] = 1
else:
int_bit_list[0] = 0
return int_bit_list
# type(number) = float
elif isinstance(number, float):
buffer = hexlify(string_at(id(number), getsizeof(number)))
buffer_float = buffer[len(buffer) - 16:]
temp = [buffer_float[i:i + 2] for i in range(0, len(buffer_float), 2)]
flt_bin_list = ''.join([bin(int(temp[i].decode(), 16))[2:].rjust(8, '0')
for i in range(len(temp) - 1, -1, -1)])
flt_bin_list = list(map(int, flt_bin_list))
return flt_bin_list
else:
raise TypeError("Don't know the precision of type %s." % type(number))
if __name__ == '__main__':
enc_result = encoding(-0.5)
print(encoding(0.5))
print(enc_result)
print(len(enc_result))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。