Disclosure: Some tools link to SaaS products we may earn a commission from — at no cost to you. All tools are free and run entirely in your browser. See full disclosure →

Why Decimal-to-Binary Matters for Developers

Understanding the binary representation of a decimal number is essential for bitwise programming, network subnetting, data type selection, and low-level debugging. When you write uint8_t x = 42 in C or np.int8(-1) in NumPy, knowing the binary layout helps you predict overflow, wrapping behaviour, and bitmask results. This converter shows the raw bit string so you can reason about the actual memory representation.

The Division-by-2 Algorithm

To convert a decimal integer to binary, repeatedly divide by 2 and record the remainders. Read the remainders from bottom to top.

Example: 42 ÷ 2 = 21 r 0. 21 ÷ 2 = 10 r 1. 10 ÷ 2 = 5 r 0. 5 ÷ 2 = 2 r 1. 2 ÷ 2 = 1 r 0. 1 ÷ 2 = 0 r 1. Remainders upward: 101010. So 42 = 0b101010.

// JavaScript
(42).toString(2)              // → "101010"
(42).toString(2).padStart(8, '0') // → "00101010" (zero-padded to 8 bits)

// Python
bin(42)         # → '0b101010'
format(42, 'b') # → '101010' (no prefix)
format(42, '08b') # → '00101010' (zero-padded to 8 bits)

Fixed-Width Binary and Data Types

Real-world computing uses fixed-width integers: uint8 (8 bits, 0–255), uint16 (16 bits, 0–65535), uint32 (32 bits), int64 (64 bits, signed). Zero-padding to 8, 16, or 32 bits shows the full memory representation of the value in those types.

The value 42 in a uint8 is 00101010 — 8 bits. In a uint16, it is 0000000000101010 — 16 bits. The leading zeros are not mathematically significant, but they reflect the actual memory layout of the variable, which matters when you are crafting binary protocols or interpreting hex dumps from a debugger.

Bitmasks and Flags

Bitmask constants are almost always defined in hex (0x01, 0x02, 0x04...) but their meaning is clearest in binary: 00000001, 00000010, 00000100. Each flag occupies one bit position. Setting flags: value | mask. Checking flags: (value & mask) !== 0. Clearing flags: value & ~mask.

Frequently Asked Questions

How do I convert a decimal number to binary in JavaScript?

Use number.toString(2). For example, (42).toString(2) returns '101010'. To zero-pad to 8 bits: (42).toString(2).padStart(8, '0') returns '00101010'. The .padStart() method is available in all modern browsers and Node.js.

What is the binary representation of 255?

255 in binary is 11111111 — all 8 bits set to 1. This is the maximum value of an unsigned 8-bit integer (uint8). In hex, it is 0xFF. In decimal, it is 255 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1.

How are negative numbers stored in binary?

Negative integers use two's complement: invert all bits and add 1. So -1 in a 32-bit system is all 1s (0xFFFFFFFF = 4294967295 if read as unsigned). -128 in 8-bit is 10000000. The most significant bit (MSB) acts as a sign indicator: 1 means negative, 0 means non-negative.

How many bits does the number 1000 need?

1000 in decimal is 1111101000 in binary — 10 bits. To know the minimum bit width needed for a number n, compute ceil(log2(n + 1)). For 1000: ceil(log2(1001)) ≈ ceil(9.97) = 10 bits.

What is zero-padding in binary, and when is it used?

Zero-padding adds leading zeros to make a binary string a fixed width. 101010 (42) padded to 8 bits becomes 00101010. This reflects the actual memory layout of fixed-width integers (uint8, uint16, uint32). Zero-padded binary is used in protocol documentation, memory dumps, and when comparing bit patterns side by side.

Number & Date Converters
Hex to Decimal Decimal to Hex Binary to Decimal RGB to Hex Hex to RGB Timestamp to Date Date to Timestamp Unix to ISO 8601 ISO 8601 to Unix
💡
Need AI tools for your development workflow?

Explore our reviews of the best AI coding assistants, documentation generators, and developer productivity tools — ranked by real-world usefulness.

Browse AI Tools for Developers →
The DevTools Team
Infinfy Engineering
We build free developer utilities that we actually use ourselves. No accounts, no tracking, no backend — just fast, accurate, in-browser tools. Part of Infinfy Solutions.