bits per character
bpc calculation
string encoding
data compression
character encoding

How to calculate bits per character of a string? bpc

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Bits per character, or BPC, is the average number of bits used to represent each character of a string in some encoded form. The key word is average, because for many encodings and compression schemes the number is not fixed per character.

The Basic Formula

The general formula is:

BPC = total encoded bits / number of characters

So if a string becomes 56 bits after encoding and contains 7 characters, the BPC is 8.

That sounds simple, but the tricky part is deciding what “total encoded bits” means for the encoding or compression method you are measuring.

Fixed-Width Encodings

For fixed-width encodings, the math is straightforward. If every character uses the same number of bits, BPC is constant.

python
1def bpc(total_bits: int, char_count: int) -> float:
2    return total_bits / char_count if char_count else 0.0
3
4print(bpc(56, 7))

In an 8-bit byte-oriented representation of 7 characters, BPC is 8.0.

This is the simple case, not the general case.

Variable-Width Encodings

Encodings such as UTF-8 are variable-width. Different characters can occupy different byte counts, so BPC depends on the actual string content.

python
1text = "Aé🙂"
2utf8_bytes = text.encode("utf-8")
3bit_count = len(utf8_bytes) * 8
4bpc_value = bit_count / len(text)
5
6print(bit_count)
7print(bpc_value)

Here the BPC is an average across the three characters, not a promise that each character used the same number of bits.

That is why BPC is useful for measuring actual data, not just describing encoding theory.

Compression Changes the Meaning

If you are calculating BPC for compression, you are no longer talking about a raw character encoding. You are measuring how efficiently the compressed representation stores the text.

For example, if a compressed file storing a 1000-character string takes 2500 bits, then the compressed BPC is 2.5.

That number can be much lower than the original encoding because repeated patterns or skewed symbol frequencies let the compression scheme store the data more efficiently.

A Practical Python Example

Here is a small helper that measures BPC for an encoded string.

python
1def bits_per_character(text: str, encoding: str = "utf-8") -> float:
2    if not text:
3        return 0.0
4    data = text.encode(encoding)
5    return (len(data) * 8) / len(text)
6
7print(bits_per_character("Example", "ascii"))
8print(bits_per_character("Aé🙂", "utf-8"))

This gives you a concrete average for the specific text and encoding you care about.

That also makes BPC a useful reporting metric when comparing encodings or compression strategies on the same dataset. You are not just describing the encoding in theory. You are measuring how densely that actual string was represented.

That distinction is what gives the metric practical value in real systems.

Without it, the number is easy to misread.

Common Pitfalls

  • Assuming BPC is always a fixed constant when many encodings are variable-width.
  • Forgetting to define whether the measurement refers to raw encoding or compressed representation.
  • Comparing strings with very different character sets as if they should produce the same average BPC.
  • Dividing by byte count instead of character count and accidentally computing the wrong metric.
  • Ignoring empty-string handling and dividing by zero.

Summary

  • Bits per character is an average: total bits divided by total characters.
  • Fixed-width encodings make BPC easy to compute, but variable-width encodings do not.
  • In UTF-8 and similar encodings, BPC depends on the actual text.
  • Compression can reduce BPC below the raw encoding cost.
  • Always define what representation you are measuring before interpreting the number.

Course illustration
Course illustration

All Rights Reserved.