5 #ifndef BITCOIN_SERIALIZE_INTCODE_H
6 #define BITCOIN_SERIALIZE_INTCODE_H
24 0x0100'0000'0000'0000,
46 template <
typename Stream>
void WriteIntcode(Stream &os, uint64_t value) {
54 const uint64_t numBits =
CountBits(value);
56 uint64_t numBytes = (numBits - 1) / 7;
63 const uint64_t header = (0xff << (8 - numBytes)) & 0xff;
65 value |= header << (8 * numBytes);
69 value <<= 8 * (8 - numBytes);
73 for (
size_t i = 0; i < numBytes; ++i) {
94 const uint64_t leadingOnes = 8 -
CountBits(uint8_t(~header));
97 const uint8_t mask = 0xff >> leadingOnes;
98 uint64_t result = header & mask;
101 for (
size_t i = 0; i < leadingOnes; ++i) {
108 "non-canonical ReadMitraInt(): 0x%016x out of range for %d "
110 result, leadingOnes));
static uint64_t CountBits(uint64_t x)
Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set.
uint8_t ser_readdata8(Stream &s)
void ser_writedata8(Stream &s, uint8_t obj)
Lowest-level serialization and conversion.
const uint64_t VALID_RANGE[]
uint64_t ReadIntcode(Stream &is)
Read a 64-bit integer as intcode, see WriteIntcode.
void WriteIntcode(Stream &os, uint64_t value)
Write a 64-bit integer in intcode encoding: