Decimal to Binary Converter
Convert decimal (base-10) numbers to binary (base-2) format. Also see hexadecimal and octal equivalents.
Related Tools
Frequently Asked Questions
How do you convert decimal to binary?
Repeatedly divide the decimal number by 2 and collect the remainders from bottom to top. For 13: 13÷2=6r1, 6÷2=3r0, 3÷2=1r1, 1÷2=0r1 → binary is 1101.
How are negative numbers represented in binary?
Negative integers are typically represented using two's complement in computing. The most significant bit is the sign bit (1 = negative). -1 in 8-bit two's complement is 11111111.
What is hexadecimal and how does it relate to binary?
Hexadecimal is base-16 (digits 0–9 and A–F). One hex digit represents exactly 4 binary bits, making it a compact way to write binary values. 0xFF = 11111111 in binary = 255 in decimal.
Decimal to Binary Conversion: Understanding Number Bases in Computing
Converting decimal numbers to binary is a foundational skill in computer science and digital electronics. Every number that a computer processes is ultimately represented in binary — a system of ones and zeros that maps directly to the physical states of transistors and memory cells. While modern programming languages handle most number representation automatically, understanding how decimal-to-binary conversion works unlocks deeper knowledge of data types, bitwise operations, memory addressing, and low-level programming.
The Division-by-Two Method
The most intuitive method for converting a decimal number to binary is the repeated division-by-two algorithm. You divide the number by 2, record the remainder (which will be 0 or 1), then divide the quotient by 2 again, recording each remainder. You continue until the quotient reaches 0. Reading the remainders in reverse order — from bottom to top — gives you the binary representation. For example, converting 13: 13÷2=6 remainder 1, 6÷2=3 remainder 0, 3÷2=1 remainder 1, 1÷2=0 remainder 1. Reading remainders bottom-up: 1101. So decimal 13 = binary 1101.
This method works because each division step determines one binary digit, starting from the least significant bit. The remainder of dividing any number by 2 reveals whether that number is odd (remainder 1) or even (remainder 0), which corresponds directly to the value of the current bit position. The successive divisions extract one bit at a time until no more information remains.
Positional Notation and Powers of Two
An alternative approach that builds stronger intuition is the subtraction method using powers of two. You identify the largest power of 2 that fits into your number, place a 1 in that bit position, subtract it from your number, and repeat with the remainder. The powers of 2 — 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 — are worth memorizing. For 13: the largest power of 2 not exceeding 13 is 8 (2³), so bit 3 is 1, remainder 5. The largest power not exceeding 5 is 4 (2²), so bit 2 is 1, remainder 1. The largest power not exceeding 1 is 1 (2⁰), so bit 0 is 1. Result: 1101.
This positional approach is often faster for mental arithmetic once you've memorized the powers of 2, and it makes the relationship between the decimal value and its binary representation more visually apparent. Each 1 in the binary number corresponds to a power of 2 that contributes to the total value. The binary representation of any number is essentially a compact way of listing which powers of 2 sum to that number.
Signed Integers and Two's Complement
Positive integers have a straightforward binary representation, but handling negative numbers requires a convention. Modern computers use two's complement to represent signed integers. In two's complement, the most significant bit (MSB) serves as a sign bit — 0 for positive, 1 for negative. To convert a positive number to its negative two's complement representation, you invert all the bits and add 1. For example, 5 in 8-bit binary is 00000101. Inverting gives 11111010. Adding 1 gives 11111011, which represents -5.
Two's complement is elegant because it allows the processor to use the same addition circuits for both positive and negative numbers. Adding 5 (00000101) and -5 (11111011) produces 100000000 — a 9-bit result where the overflow carry is discarded, leaving 00000000, which is correctly zero. This simplicity in hardware is why two's complement became the universal standard for signed integer representation in virtually all modern computer architectures.
Fixed-Width Binary and Overflow
In real computing systems, binary numbers are stored in fixed-width formats: 8-bit bytes, 16-bit words, 32-bit integers, or 64-bit long integers. This fixed width creates both a maximum and minimum representable value and introduces the concept of overflow. An 8-bit unsigned integer can hold values from 0 to 255. If you try to store 256 in 8 bits, the ninth bit is lost and you get 0 — the number wraps around. This is why integer overflow is a significant source of bugs in low-level programming and an attack vector in security vulnerabilities.
Understanding the relationship between decimal values and their binary representations in fixed-width formats helps you reason about data types. When a function returns an int32 and you're working with values that might exceed 2,147,483,647 (the maximum 32-bit signed integer), you know you need a larger type. When working with network protocols or binary file formats that specify field widths in bits, knowing how decimal values fit into those bit widths is essential for correct implementation.
Binary in Programming Practice
Decimal-to-binary conversion is directly relevant to everyday programming tasks involving bitwise operations. Bitmasks are a common technique for packing multiple boolean flags into a single integer. If you define flags as powers of 2 — FLAG_A = 1 (binary 0001), FLAG_B = 2 (binary 0010), FLAG_C = 4 (binary 0100) — you can combine them with bitwise OR and test them with bitwise AND. Understanding the binary representation of these flag values makes it easy to reason about which flags are set without a calculator.
File permissions in Unix systems are another practical application: the permission value 755 represents rwxr-xr-x in binary (111 101 101), where each group of three bits encodes read, write, and execute permissions for owner, group, and other respectively. Network subnet masks, color channels in packed pixel formats, hardware status registers, and protocol field definitions all rely on binary reasoning. Fluency in decimal-to-binary conversion underpins all of these areas and makes you a more effective systems programmer.
Floating Point: Binary for Fractional Numbers
Decimal-to-binary conversion extends to fractional numbers as well, though the process is less intuitive. For the fractional part, you repeatedly multiply by 2 and collect the integer part of each result as the next binary digit. The notorious imprecision of floating-point arithmetic — where 0.1 + 0.2 does not equal exactly 0.3 in most programming languages — arises because many decimal fractions cannot be represented exactly in binary. Just as 1/3 has no finite decimal representation, 1/10 has no finite binary representation. Understanding this fundamental limitation of binary fractions helps you write more robust numerical code and choose appropriate comparison strategies when working with floating-point values.