jimmysong / programmingbitcoin

Repository for the book
Other
1.75k stars 656 forks source link

Chapter 5: In function encode_varint - Why use < 0xfd instead of < 0x100 for one byte strings, similar to the larger options? #274

Closed derekpa closed 1 year ago

derekpa commented 1 year ago

Why not use the full byte capacity or the first one byte option, selecting values < 0xfd instead of < 0x100?

Thanks.

if i < 0x100: return bytes([i]) elif i < 0x10000: return b'\xfd' + int_to_little_endian(i, 2)

instead of

if i < 0xfd: return bytes([i]) elif i < 0x10000: return b'\xfd' + int_to_little_endian(i, 2)

derekpa commented 1 year ago

OK, I assume its because 0xfd to 0xff are used as flags. Makes sense.