Bit and Byte Length Calculator
Calculate the bit and byte length of an integer, hexadecimal string, or text string, with formulas for UTF-8, UTF-16, UTF-32, ASCII, and Latin-1 encoding.
Bit and Byte Length Calculator
8 bits = 1 bytes
Visualization
Documentation
What is a bit and byte length calculator?
A bit and byte length calculator finds how many bits and bytes a piece of data needs. A bit is a single binary digit, 0 or 1. A byte is a group of 8 bits. The calculator accepts an integer, a hexadecimal string, or a text string, and reports the exact size in both units.
This matters for programming, networking, and data storage. A database column, a network packet, and a file on disk all have a size measured in bytes, and that size depends on how the data is written.
How to calculate the bit length of an integer
The bit length of a positive integer is the number of digits in its binary form. For example, 255 in binary is 11111111, which has 8 digits, so its bit length is 8.
Formula: for a positive integer n, the bit length is the number of digits in the base-2 representation of n. The special case n = 0 has a bit length of 1.
Negative integers use two's complement, the standard way computers store signed numbers. The bit length of a negative integer is the smallest number of bits that can hold it in two's complement form. For example, β128 fits in 8 bits (as 10000000), but β129 needs 9 bits, because 8 bits can only reach down to β128.
Once the bit length is known, the byte length is found by rounding up to the nearest whole byte:
Byte length = βbit length Γ· 8β
The symbol β β means "round up." A 9-bit value still needs 2 full bytes, because a byte cannot be split.
Worked examples
| Integer | Bit length | Byte length |
|---|---|---|
| 0 | 1 | 1 |
| 255 | 8 | 1 |
| 256 | 9 | 2 |
| β128 | 8 | 1 |
| β129 | 9 | 2 |
| 18,446,744,073,709,551,615 (2βΆβ΄ β 1) | 64 | 8 |
How to calculate the bit length of a hexadecimal string
Each hexadecimal digit (0β9, AβF) represents exactly 4 bits, because 4 bits can hold 16 possible values.
Formula: bit length = number of hex digits Γ 4. Byte length is again the bit length divided by 8, rounded up.
For example, the hex string "FF" has 2 digits, so its bit length is 2 Γ 4 = 8 bits, or 1 byte. The hex string "ABCD" has 4 digits, giving 16 bits, or 2 bytes. Spaces inside the string are ignored before counting.
How to calculate the byte length of a text string
For text, the size depends on the character encoding, the rule used to turn characters into bytes. The calculator supports five encodings:
- UTF-8: a variable-length encoding used across the web. Each character takes 1 to 4 bytes. Plain English letters, digits, and punctuation take 1 byte each.
- UTF-16: used internally by JavaScript and Windows. Most characters take 2 bytes; characters outside the Basic Multilingual Plane, such as many emoji, take 4 bytes.
- UTF-32: a fixed-length encoding. Every character takes exactly 4 bytes, no matter which one it is.
- ASCII: a 7-bit encoding covering 128 characters, mainly English letters, digits, and basic punctuation. Each character takes 1 byte. Any character outside this set is rejected.
- Latin-1 (ISO-8859-1): an 8-bit encoding covering 256 characters, adding accented letters used in Western European languages. Each character takes 1 byte. Characters outside this set are rejected.
Formula: byte length = number of bytes the encoding produces for the string. Bit length = byte length Γ 8.
Worked examples
The text "Hello, world!" has 13 characters, all plain ASCII letters and punctuation.
- UTF-8: 13 bytes (104 bits), since every character fits in 1 byte.
- UTF-16: 26 bytes (208 bits), since every character takes 2 bytes.
The text "γγγ«γ‘γ―δΈη" (seven Japanese characters) takes 3 bytes per character in UTF-8, giving 21 bytes (168 bits).
An emoji such as π lies outside the Basic Multilingual Plane. It takes 4 bytes in UTF-8, 4 bytes in UTF-16 (as a surrogate pair), and 4 bytes in UTF-32.
Why signed integers change the answer
Computers store negative numbers with two's complement rather than a plain minus sign. This means the bit length of a negative integer is not simply the bit length of its magnitude plus one. The rule is: the bit length is the smallest number of bits k such that the value is at least β2^(kβ1). That is why β128 (which equals β2β·) fits in 8 bits, while β129 needs 9.
Frequently asked questions
How do I convert bytes to bits? Multiply by 8. Ten bytes equal 80 bits, because each byte holds 8 bits by definition.
What is the difference between bit length and byte length? Bit length counts individual binary digits. Byte length counts groups of 8 bits, rounded up to the next whole byte when the bit length is not a multiple of 8.
How many bytes does a UTF-8 character use? Between 1 and 4. Characters shared with ASCII use 1 byte. Many accented Latin, Greek, and Cyrillic characters use 2 bytes. Most Chinese, Japanese, and Korean characters use 3 bytes. Emoji and other rare symbols use 4 bytes.
How is the byte length of a hex string calculated? Count the hex digits after removing spaces, then divide by 2 (since each byte is 2 hex digits, or 8 bits). "FF" is 1 byte; "ABCD" is 2 bytes.
Why does a negative integer sometimes need more bits than its positive counterpart? Two's complement uses one bit to mark the sign, but that bit is shared with the value's range rather than added on top for every case. β128 fits in the same 8 bits as positive values up to 127, but β129 no longer fits in 8 bits and needs 9.
What input sizes does this calculator accept? Integers, hex strings, and text strings up to 1,000,000 characters long.
References
- "Character encoding." Wikipedia, Wikimedia Foundation. https://en.wikipedia.org/wiki/Character_encoding
- "UTF-8, UTF-16, UTF-32 & BOM." Unicode, Inc. https://www.unicode.org/faq/utf_bom.html
- "Two's complement." Wikipedia, Wikimedia Foundation. https://en.wikipedia.org/wiki/Two%27s_complement