Binary Numbers Explained for Programmers
Learn how binary numbers work: place values, counting, converting and why every integer in a program is a pattern of bits, with C code that prints them.

Every value a computer holds is a pattern of two-state switches. A switch is either off or on, and we write those states as 0 and 1. Binary is the number system built on exactly those two digits, and it is the first thing worth understanding before registers, masks, overflow or floating point make sense. This post defines the terms, shows how counting works, and ends with a small C program that prints any integer as bits. The C examples assume GCC 14 or Clang 18 in the C17 mode (-std=c17), and nothing here depends on a particular operating system.
What a binary number is
You already know a positional number system: decimal. In the number 305, each digit has a place, and each place is worth ten times the one to its right. The 5 is worth 5 times 1, the 0 is worth 0 times 10, and the 3 is worth 3 times 100. Add them up and you get three hundred and five. The number ten is the base, or radix, of the system.
Binary keeps the idea and changes the base to two. Each place is worth two times the place to its right, so the places from the right are 1, 2, 4, 8, 16, 32, 64, 128 and so on. A single binary digit is called a bit, short for binary digit. The bit on the far right is the least significant bit, because it is worth the least. The bit on the far left is the most significant bit.
Take the binary number 101101. Reading from the right, the places are 1, 2, 4, 8, 16, 32, and the digits sit on them like this:
| Place value | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|
| Bit | 1 | 0 | 1 | 1 | 0 | 1 |
Add the place values where the bit is 1: 32 plus 8 plus 4 plus 1 gives 45. So binary 101101 is decimal 45. That is the whole conversion from binary to decimal: sum the weights of the set bits.
Counting in binary
Counting works the same way as in decimal, only you run out of digits sooner. In decimal you count 0 to 9 and then roll over: the units digit resets to 0 and a 1 appears in the tens place. In binary you count 0, then 1, and the next step already rolls over: the units digit resets to 0 and a 1 appears in the twos place.
| Decimal | Binary |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
| 8 | 1000 |
Two patterns are worth memorising. First, every power of two is a single 1 followed by zeros: 4 is 100, 8 is 1000. Second, one less than a power of two is all ones: 7 is 111 and 15 is 1111. The second pattern explains why the largest value in n bits is 2 to the power n, minus 1. Eight bits count from 0 to 255, because 2 to the power 8 is 256.
How many values fit in n bits
Each extra bit doubles the number of patterns, since the new bit can be 0 or 1 in front of every earlier pattern. One bit gives 2 patterns, two bits give 4, three give 8, and so on. With n bits you get 2 to the power n distinct patterns. If you treat the patterns as unsigned numbers, they represent 0 up to 2 to the power n minus 1.
| Bits | Patterns | Largest unsigned value |
|---|---|---|
| 4 | 16 | 15 |
| 8 | 256 | 255 |
| 16 | 65,536 | 65,535 |
| 32 | 4,294,967,296 | 4,294,967,295 |
This is the reason integer types have limits. A variable of a fixed width has only so many patterns, and when arithmetic needs one more than exists, the result is a different story that the later posts in this series cover. For now the point is that the width of a value is a physical fact about the hardware or the type, not a detail the program can ignore.
Converting decimal to binary
Going the other way, from decimal to binary, uses repeated division by two. Divide the number by 2, write down the remainder, and repeat with the quotient until the quotient is zero. The remainders, read from last to first, are the bits.
Convert 45 as an example:
| Step | Number | Divided by 2 | Remainder |
|---|---|---|---|
| 1 | 45 | 22 | 1 |
| 2 | 22 | 11 | 0 |
| 3 | 11 | 5 | 1 |
| 4 | 5 | 2 | 1 |
| 5 | 2 | 1 | 0 |
| 6 | 1 | 0 | 1 |
Reading the remainders from the bottom up gives 101101, which matches the earlier example. The first remainder you write is the least significant bit, which is why you read the column backwards. A separate post covers the hand method in more depth and adds the hexadecimal shortcut.
Printing the bits of an integer in C
C has no standard format specifier for binary in printf through C17, so you print bits yourself. The idea is to test each bit from the most significant down to the least significant, using a shift to move the bit of interest into the lowest place and a mask of 1 to isolate it.
#include <stdio.h>
#include <stdint.h>
static void print_bits(uint8_t value)
{
for (int i = 7; i >= 0; i--) {
putchar(((value >> i) & 1u) ? '1' : '0');
}
putchar('\n');
}
int main(void)
{
print_bits(45);
print_bits(255);
print_bits(1);
print_bits(128);
return 0;
}The loop runs i from 7 to 0. The expression value >> i shifts the byte right by i places, so the bit that was at position i lands in position 0, and & 1u keeps only that bit. A result of 1 prints as the character 1, anything else as 0. Because the loop starts at the high end, the output reads left to right in the same order you would write the number.
00101101
11111111
00000001
10000000The first line is 45 padded to eight bits. Writing leading zeros is a display choice: the value 45 is the same whether you show 101101 or 00101101, but a fixed-width view makes the width of the type visible, and that is usually what you want when you are debugging.
C23, which GCC 14 and later accept with -std=c23, adds binary literals such as 0b101101 to the language, and the %b conversion arrived in the C23 library and in glibc 2.35. GCC and Clang have accepted the 0b literal as an extension for much longer, but a portable program that must build with older compilers should keep printing bits by hand as above.
Bits have no meaning on their own
A pattern like 01000001 is just eight bits. Whether it means the number 65, the letter A in ASCII, a colour channel in a pixel, or a status flag depends entirely on how the program interprets it. The hardware does not remember the intent. The type in your source code, the format of the file, or the protocol decides. This is why one of the most useful debugging habits is to look at the raw bits and ask what the interpretation should have been.
The same holds for width. The byte 11111111 is 255 as an unsigned value and negative one as a signed value in the usual two's complement scheme. Nothing in the pattern tells you which. The post on two's complement explains that scheme, and the post on signed and unsigned integers shows how C picks between the two readings.
Common mistakes
Reading a binary number left to right and forgetting that the rightmost bit is the smallest. Always start the weights from the right.
Dropping leading zeros when the width matters. In a register, a protocol field or a mask, 00000101 and 101 are the same number but different widths.
Assuming a byte is always eight bits. It is on every mainstream platform today, and C exposes the real figure as
CHAR_BITinlimits.h, which POSIX fixes at 8.Shifting by a number of places greater than or equal to the width of the type. In C that is undefined behaviour, and a later post shows how to avoid it.


