MCPcopy Index your code
hub / github.com/petertodd/python-bitcoinlib / compact_from_uint256

Function compact_from_uint256

bitcoin/core/serialize.py:318–335  ·  view source on GitHub ↗

Convert uint256 to compact encoding

(v)

Source from the content-addressed store, hash-verified

316 return v
317
318def compact_from_uint256(v):
319 """Convert uint256 to compact encoding
320 """
321 nbytes = (v.bit_length() + 7) >> 3
322 compact = 0
323 if nbytes <= 3:
324 compact = (v & 0xFFFFFF) << 8 * (3 - nbytes)
325 else:
326 compact = v >> 8 * (nbytes - 3)
327 compact = compact & 0xFFFFFF
328
329 # If the sign bit (0x00800000) is set, divide the mantissa by 256 and
330 # increase the exponent to get an encoding without it set.
331 if compact & 0x00800000:
332 compact >>= 8
333 nbytes += 1
334
335 return compact | nbytes << 24
336
337def uint256_to_str(u):
338 r = b""

Callers 2

test_twelveMethod · 0.85
test_from_uint256Method · 0.85

Calls

no outgoing calls

Tested by 2

test_twelveMethod · 0.68
test_from_uint256Method · 0.68