Disclosure: this post was written with AI assistance and checked by the FlashKit team. Every number below was produced by running the code shown.

Most developers can call bin() or toString(2) without thinking. Fewer can say what those functions are doing, or why -45 turns into 11010011 in one place and -101101 in another. This post covers the conversions by hand first, then the same steps in JavaScript and Python, then the sharp edges: fixed widths, negative numbers and a few common mistakes.

Each binary digit (bit) is a remainder. Divide the number by 2, write down the remainder, and repeat with the quotient until the quotient is 0. The remainders, read from last to first, are the binary number.

According to recent reports, read the remainders from the bottom up: 10011100. The first remainder is the least significant bit, which is why the order flips. If you would like to try other numbers and see the remainder table, the decimal to binary converter shows the same steps.

Going the other way, every position has a weight that is a power of 2, doubling as you move left. Take 10110101:.

Add the weights where the bit is 1: 128 + 32 + 16 + 4 + 1 = 181.

For the part after the decimal point, multiply by 2 instead of dividing. The whole-number part of each result is the next bit; keep the fractional part and repeat. For 0.625:.

So 0.625 is 0.101 in binary, and 10.625 is 1010.101. Many fractions never reach 0. For example 0.1 starts 0.000110011001... and repeats forever, which is why it cannot be stored exactly in a float. I covered that in How floating-point numbers are stored, so the rest of this post sticks to integers.

It has been revealed that computers store integers in fixed widths, usually 8, 16, 32 or 64 bits. A fixed width means you pad the left with zeros. 5 is 101 as a bare number, but in 8 bits it is 00000101, in 16 bits 0000000000000101, and in 32 bits 00000000000000000000000000000101.

According to recent reports, the width also limits what fits. An unsigned n-bit value goes from 0 to 2^n - 1:.

The number 300 needs 9 bits (100101100), so it does not fit in 8 bits at all.

Recent findings suggest that binary has no minus sign in memory, so signed integers use two's complement. To write a negative number at a given width:.

So -45 is 11010011. As an unsigned byte that same pattern is 211, which is 256 - 45. That is the whole idea: at width n, the negative number -x is stored as 2^n - x.

To read a pattern back, check the top bit. If it is 0, read it normally. If it is 1, the top bit counts as a negative weight (-128 in 8 bits):.

11010011 = -128 + 64 + 16 + 2 + 1 = -45.

Or, equivalently, take the unsigned value (211) and subtract 256.

In a recent development, the same -45 at other widths is just sign extension: 1111111111010011 in 16 bits and 11111111111111111111111111010011 in 32 bits. If you want to check a value at 4, 8, 16, 32 or 64 bits without doing it on paper, the two's complement calculator converts both ways.

Four bits make one hex digit, so hex is just binary grouped in fours. Split 10011100 into 1001 1100. That is 9 and C (12), so 156 is 0x9C. Going back, 0xB5 is 1011 0101, which is 10110101, or 181. This is why hex is the usual way to write bytes and bit masks.

According to recent reports, and the division method, written out. It mirrors the table above:.

Negative numbers are where JavaScript surprises people. (-45).toString(2) gives "-101101", a minus sign plus the magnitude, not two's complement. Mask to a width to get the real bit pattern:.

This works up to width 31. JavaScript's bitwise operators work on 32-bit signed integers, so for 32-bit patterns use (n >>> 0).toString(2), and for 64-bit use BigInt: BigInt.asUintN(64, -45n).toString(2).

It has been revealed that python integers have no fixed width, so there is no "natural" two's complement. bin(-45) gives '-0b101101'. Masking with & (2**width - 1) produces the fixed-width pattern, because Python treats negative integers as having infinitely many leading 1 bits:.

Do a few by hand, then compare against a tool. The binary converter demonstrates decimal, binary, hex and octal side by side and lets you choose signed or unsigned and the bit width. It is a quick way to confirm that your manual loop and your masks agree. For the reverse direction with a positional sum, see the binary to decimal converter.

If you have a conversion that behaves oddly in your own language, leave it in the comments and I will take a look.

β†’ Read original article