MCPcopy
hub / github.com/jimmysong/programmingbitcoin / encode_num

Function encode_num

code-ch08/op.py:20–39  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

18
19
20def encode_num(num):
21 if num == 0:
22 return b''
23 abs_num = abs(num)
24 negative = num < 0
25 result = bytearray()
26 while abs_num:
27 result.append(abs_num & 0xff)
28 abs_num >>= 8
29 # if the top bit is set,
30 # for negative numbers we ensure that the top bit is set
31 # for positive numbers we ensure that the top bit is not set
32 if result[-1] & 0x80:
33 if negative:
34 result.append(0x80)
35 else:
36 result.append(0)
37 elif negative:
38 result[-1] |= 0x80
39 return bytes(result)
40
41
42def decode_num(element):

Callers 15

op_checkmultisigFunction · 0.90
op_0Function · 0.70
op_1negateFunction · 0.70
op_1Function · 0.70
op_2Function · 0.70
op_3Function · 0.70
op_4Function · 0.70
op_5Function · 0.70
op_6Function · 0.70
op_7Function · 0.70
op_8Function · 0.70
op_9Function · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected