一些在其它语言可以轻易实现的功能,但是在python中会有点误差的实现方案
这里的内容会持续更新,新增!
JS板块 一. 浮点数转二进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 def dectbin (num ): if num == int (num): integer = '{:b}' .format (int (num)) return integer else : integer_part = int (num) decimal_part = num - integer_part integercom = '{:b}' .format (integer_part) tem = decimal_part tmpflo = [] A = True while A: tem *= 2 tmpflo += str (int (tem)) if tem > 1 : tem -= int (tem) elif tem < 1 : pass else : break flocom = tmpflo return integercom + '.' + '' .join(flocom) if __name__ == '__main__' : number = 5 result = dectbin(number) print (f'{number} 的二进制数为:{result} ' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 import ctypesdef xor (x, y ): x, y = ctypes.c_int32(x).value, ctypes.c_int32(y).value return ctypes.c_int(x ^ y).value def unsigned_right_shift (x, y ): x, y = ctypes.c_uint32(x).value, y % 32 return ctypes.c_uint32(x >> y).value def left_shift (x, y ): x, y = ctypes.c_int32(x).value, y % 32 return ctypes.c_int32(x << y).value def right_shift (x, y ): x, y = ctypes.c_int32(x).value, y % 32 return ctypes.c_int32(x >> y).value def dectbin (num ): if num == int (num): integer = '{:b}' .format (int (num)) return integer else : integer_part = int (num) decimal_part = num - integer_part integercom = '{:b}' .format (integer_part) tem = decimal_part tmpflo = [] A = True while A: tem *= 2 tmpflo += str (int (tem)) if tem > 1 : tem -= int (tem) elif tem < 1 : pass else : break flocom = tmpflo return integercom + '.' + '' .join(flocom)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 def conversion_of_binary (number ): global binary_integer_part, binary_fractional_part, binary_total, integer_part, fractional_part integer_part = int (number) binary_integer_part = bin (integer_part)[2 :] fractional_part = number - integer_part binary_fractional_part = conversion_of_fraction(fractional_part, 2 ) binary_total = binary_integer_part + '.' + binary_fractional_part return binary_total def conversion_of_fraction (num, base ): binary = '' while num != 0 : num = num * base binary = binary + str (int (num)) num = num - int (num) return binary if __name__ == "__main__" : result = conversion_of_binary(2.8 )
二、逻辑运算转换 在 JavaScript 内部,数值都是以 64 位浮点数的形式储存,但是做位运算的时候,是以 32 位带符号的整数进行运算的,并且返回值也是一个 32 位带符号的整数。目的是使在 python 中的显示和 js 中的显示相同,就在 Python 中做一次从 uint32 到 int32 的转换。
1.逻辑符号 ^
1 2 3 4 5 6 import structnum = 3988292384 ^ 127 print (num) print (struct.unpack('i' , struct.pack('I' , num))[0 ])
1 2 3 4 5 6 7 def int_overflow (val ): maxint = 2147483647 if not -maxint-1 <= val <= maxint: val = (val + (maxint + 1 )) % (2 * (maxint + 1 )) - maxint - 1 return val print (int_overflow(3988292384 ^ 127 ))
2.无符号右移 >>> 这里目前还没有完美的方案,数据量非常大的时候可能需要加上 板块二 逻辑符号 ^ 里面数据量过大情况进行数据修正,大概意思就是这个得出来的结果再套一层。
示例 :采用逻辑符号 ^ 方案二 –> int_overflow(这里就是无符号右移得出来的结果)
1 2 3 4 5 6 7 8 def unsinged_right_shift (x, y ): x = x & 0xffffffff signed = False if x < 0 : signed = True x = x.to_bytes(4 , byteorder='big' , signed=signed) x = int .from_bytes(x, byteorder='big' , signed=False ) return x >> (y & 0xf )
1 2 3 4 5 6 import ctypesdef unsinged_right_shift (x, y ): x,y = ctypes.c_uint32(x).value,y % 32 return ctypes.c_uint32(x >> y).value
1 2 3 4 5 6 7 MAX32INT = 4294967295 def unsinged_right_shift (num, bit=0 ) -> int : val = ctypes.c_uint32(num).value >> bit return (val + (MAX32INT + 1 )) % (2 * (MAX32INT + 1 )) - MAX32INT - 1
1 2 3 def unsinged_right_shift (signed, i=0 ): shift = signed % 0x100000000 return shift >> i