Convert Decimal to Binary in C and C++
Divide by 2, keep the remainders, read them backwards: 13 becomes 1101.
- Repeated division by 2
- Collecting remainders
- Printing the digits in reverse
13 / 2 = 6remainder 1lowest bit6 / 2 = 3remainder 03 / 2 = 1remainder 11 / 2 = 0remainder 1highest bit
Remainders give the bits
The problem
Computers store everything in binary, and understanding how a number like 13 becomes 1101 helps with bit masks, file formats and debugging. The conversion produces digits in reverse order, which needs handling.
Think of it like this
Converting to binary is like splitting a pile of coins into pairs again and again. Each time, you note whether one coin is left over (1) or not (0). The notes come out from the last digit to the first, so you read them backwards.
value % 2🪙 Is one coin left over?value /= 2👫 Keep only the pairsbits[count++]📝 Write down the leftoverbits[--count]🔙 Read the notes backwards: 1101Converting a decimal number to binary repeatedly divides it by 2, records each remainder, and reads the remainders in reverse order.
Important points
value % 2gives the lowest binary digit, andvalue /= 2removes it.bits[count++]stores the digits from right to left.- The
do-whileloop runs at least once, so 0 prints as"0". bits[--count]prints the stored digits in reverse, giving 1101.
When to use it
- Understanding binary numbers and bit operations.
- Printing numbers in other bases, such as 8 or 16.
- Practicing arrays used as a stack.
When not to use it
- C++ code:
std::bitset<8>(13)prints binary directly. - Negative numbers: they need two's complement handling.
C and C++ code examples
#include <stdio.h>
int main(void) {
unsigned number = 13, value = number; int bits[32], count = 0;
// Store remainders generated from right to left.
do { bits[count++] = value % 2; value /= 2; } while (value);
printf("%u = ", number); while (count) printf("%d", bits[--count]); puts("");
}
13 = 1101
#include <bitset>
#include <iostream>
#include <string>
int main() {
unsigned number = 13;
// Remove leading zeroes from a fixed-width bitset string.
std::string bits = std::bitset<32>(number).to_string();
bits.erase(0, bits.find_first_not_of('0'));
std::cout << number << " = " << bits << '\n';
}
13 = 1101
C and C++ comparison
Dividing 13 by 2 again and again gives the remainders 1, 0, 1, 1, which are the binary digits from right to left. The do-while loop stores them in bits. Printing the array backwards shows 1101.
C exposes small procedural APIs and makes representation details explicit.
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.
Common mistakes
Printing digits in the order found
The first remainder is the last digit, so 13 would print as 1011.
while (value) { printf("%u", value % 2); value /= 2; }
do { bits[count++] = value % 2; value /= 2; } while (value);
while (count) printf("%d", bits[--count]);
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges