TeachMeBitcoin

Bit Grouping (5-bit chunks)

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Bit Grouping: The 5-Bit Transformation

The core mathematical engine of Bech32 is the conversion of standard 8-bit bytes into 5-bit chunks. This process is often called "Squashing" or "Bit Grouping."

1. The 8-to-5 Problem

Computers store data in bytes (8 bits). However, the Bech32 alphabet only has 32 characters, which can only represent 5 bits of information ($2^5 = 32$).

2. The Conversion Process

Imagine we have three bytes (24 bits): 11111111 00000000 10101010. To convert to 5-bit chunks, we simply regroup them:

  1. 11111 (Group 1)

  2. 11100 (Group 2)

  3. 00000 (Group 3)

  4. 01010 (Group 4)

  5. 1010? (Group 5 - needs padding)

3. Padding

Since the total number of bits might not be a perfect multiple of 5, the algorithm adds zero-bits at the end to complete the final 5-bit group.

4. Why this is efficient?

Because 5 and 8 are both small integers, this bit-shifting can be done using simple bitwise operators (\u003c\u003c, \u003e\u003e, \u0026).

Data Type Bit Count 5-Bit Groups
PK Hash 160 Bits 32 Groups
Script Hash 256 Bits 52 Groups
Taproot Key 256 Bits 52 Groups

In the final section, we will build a Python Bech32 Auditor.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!