Decimal to 8-bit binary: steps, examples and signed limits
An unsigned 8-bit integer represents 0–255. Convert a nonnegative whole number to base two, then add leading zeros until there are eight bits. For example, 13 becomes 00001101, 42 becomes 00101010 and 173 becomes 10101101. A value such as 256 needs nine bits and cannot fit in an unsigned byte without an explicit overflow rule.
Use the binary, decimal, octal and hex converter to check an integer and see its byte interpretation where applicable. The tool preserves long integer digits; it does not silently truncate a value to eight bits.
Repeated division by two: convert 13
Divide by two repeatedly, keeping each remainder. The first remainder is the rightmost, least significant bit. Read the remainders from bottom to top once the quotient reaches zero.
| Dividend | Integer quotient | Remainder |
|---|---|---|
| 13 | 6 | 1 |
| 6 | 3 | 0 |
| 3 | 1 | 1 |
| 1 | 0 | 1 |
Read bottom to top: 1101
Pad to eight positions: 00001101
Check: 8 + 4 + 1 = 13
Padding adds positions with value zero. It does not change the integer. Decimal zero is a special easy case: its padded representation is 00000000.
Place values: convert 173
The eight positions have values 128, 64, 32, 16, 8, 4, 2 and 1. Starting with 128, select a position when it fits into the amount that remains.
Place value: 128 64 32 16 8 4 2 1
Bit: 1 0 1 0 1 1 0 1
Selected sum: 128 + 32 + 8 + 4 + 1 = 173
Eight-bit result: 10101101
Binary and hexadecimal provide two notations for the same integer. Grouping this byte into four-bit groups gives 1010 1101, or hexadecimal AD.
Unsigned and signed bytes use different interpretations
Eight bits have 256 possible patterns. Unsigned interpretation assigns them values 0–255. Signed two’s-complement interpretation assigns values -128–127. For an unsigned pattern value u at least 128, the signed two’s-complement value is u − 256.
| Pattern | Unsigned | Signed two’s complement |
|---|---|---|
00000000 | 0 | 0 |
01111111 | 127 | 127 |
10000000 | 128 | -128 |
10101101 | 173 | -83 |
11111111 | 255 | -1 |
A leading one does not automatically make a binary integer negative. The stored width and chosen signed representation determine that interpretation. Ordinary text such as -101 is a minus sign applied to a magnitude, not the eight-bit two’s-complement encoding of -5.
Check the values in Python
for n in [0, 13, 17, 42, 85, 127, 128, 173, 255]:
assert 0 <= n <= 255
print(n, format(n, '08b'))
assert format(13, '08b') == '00001101'
assert format(173, '08b') == '10101101'
assert int.from_bytes(bytes([173]), 'big', signed=False) == 173
assert int.from_bytes(bytes([173]), 'big', signed=True) == -83
assert (-83).to_bytes(1, 'big', signed=True) == bytes([173])
# (256).to_bytes(1, 'big') raises OverflowError.
The formatting width in '08b' is a minimum width; it does not reject a value with nine or more bits. Check the numeric range when a byte is required. A one-byte value has no inter-byte ordering issue; endianness becomes relevant when interpreting multiple bytes together.
Practice and common mistakes
Try 29, 64 and 99 before checking the answers: 00011101, 01000000 and 01100011. Rebuild each decimal from its selected place values. Do not read division remainders in their original top-to-bottom order or discard a ninth bit without an explicit rule.
This guide covers integers. Binary fractions and floating-point encodings require additional rules. For the distinction between byte counts and visible characters in text, use the UTF-8 and Unicode counter; one displayed character is not necessarily one byte.
Primary reference
The arithmetic, examples and executable assertions above were independently checked. Python’s reference documents integer conversion, byte width, signed two’s-complement interpretation and overflow behavior.