Technology

Binary Calculator

Binary is where all computation ends up, whatever notation you type in. Convert decimal to binary, apply bitwise operations, and read the result in decimal, hex and octal at once — with the signed range for the width shown, because that is where the overflow bugs live.

Result
—
Binary
—
Decimal
—
Signed value
—
Hexadecimal
—
Octal
—
Range check
—
Bit width
—

Every notation is the same number

Converting between bases is just writing the same quantity in a different positional notation, exactly as 12 in base ten and "1100" in base two are the same twelve.

Decimal to binary: divide by 2 repeatedly, read the remainders bottom-up. 100 gives 1100100. Binary to decimal: sum the powers of 2 for each 1. 1100100 = 64 + 32 + 4 = 100.

For anything wider than a byte, work through hex instead, because one hex digit is exactly four bits. 100 decimal is 0x64, and 6=0110, 4=0100, so 0x64 = 0110 0100 = 100. The hex route turns eight binary digits into two, and a typo in one hex digit is far easier to spot than in a string of bits.

Why two's complement changes what a bit pattern means

This is where representation stops being counting. A byte has 256 distinct bit patterns but they can be read two ways:

  • Unsigned: 0 to 255. 0xFF is 255.
  • Signed two's complement: −128 to +127. 0xFF is −1.

The negative half works by inverting the positive bits and adding 1: +1 is 00000001, so −1 is 11111111. −5: invert 00000101 to 11111010, add 1 to get 11111011 = 0xFB.

The reason this scheme won is that addition becomes subtraction with no special case. 5 + (−1) = 00000101 + 11111111 = 1 00000100, and dropping the carry out of 8 bits leaves 00000100 = 4. The hardware does not know it is subtracting.

The range is asymmetric, and that is not a mistake

8-bit signed runs −128 to +127, not −127 to +127. The extra negative value exists because 10000000 = −128 while +128 has no pattern available. The same shape repeats at every width: 16-bit gives −32,768 to +32,767, 32-bit gives −2,147,483,648 to +2,147,483,647.

And this asymmetry is the bug. In 1996, flight software on a prototype aircraft used a 16-bit integer to hold altitude, wrapping at 32,767 feet. The aircraft climbed through 32,768, the value became −32,768, the control system saw a large negative altitude, and the aircraft rolled over. The arithmetic was entirely correct; the width was not. The altitude was 65,535 feet at the time of the accident, exactly the top of the 16-bit range.

Bitwise operations are not arithmetic

Each bit is handled independently, with no carrying between positions — that is the difference from + and ×.

  • AND — a bit survives only if it is 1 in both. flags & 0x04 is non-zero if bit 2 is set: this is how you test a flag.
  • OR — a bit is set if it is 1 in either. This is how you set one.
  • XOR — flips the bit. XOR with 0xFF is bitwise NOT; XOR with an all-ones mask is how a checksum is built.
  • Shifts — << 1 doubles, >> 1 halves. Whether the top bit is discarded or sign-extended depends on the variable's type.

The classic typo: x & 0x0F and x && 0x0F differ by one character and mean entirely different things. The first is a bitmask returning 0-15. The second is a logical AND returning true or false, and with a non-zero right-hand side it is simply true. It compiles in most languages, and it is the most common bitwise mistake there is.

Masks worth recognising

  • 0xFF — the low byte. x & 0xFF keeps one byte and discards the rest, which is why it appears in every network and file-format parser.
  • 0x0F — the low nibble, one hex digit.
  • 0x7F — clears the sign bit of a byte, the standard way to take an absolute value.
  • 0x80000000 — the sign bit of a 32-bit value, used in negation and in Math.abs implementations.

And when a value is too wide: 0xFF for a byte, 0xFFFF for 16-bit, 0xFFFFFFFF for 32-bit. A read into a narrower type than the data needs is the classic source of a plausible-but-wrong number, and the fix is always an explicit cast rather than an implicit narrowing.

Frequently asked questions

How do I convert decimal to binary?

Divide by 2 repeatedly and read the remainders bottom to top. 100 gives 1100100. For numbers wider than a byte, go via hex instead — divide by 16 and read up, which is far less error-prone than handling 32 individual bits.

Why is 0xFF sometimes 255 and sometimes -1?

Both readings use the same bits. Unsigned 8-bit interprets 11111111 as 255; signed two's complement interprets it as -1, because inverting 00000001 and adding 1 gives 11111111. The type declaration decides, not the data.

What is the range of a signed integer? If a 12-bit signed integer has a range of -2048 to 2047, and 2047 is 0 to 2047, what is the range of a 12-bit unsigned integer?

A 12-bit unsigned integer runs 0 to 4,095. The signed version has 4,096 patterns split as 2,048 negative and 2,048 non-negative, giving -2,048 to 2,047. The signed range is always one value short at the top, which is the cost of reserving one pattern for zero.

What is two's complement used for?

The standard way to represent negative numbers in binary, used by essentially every computer. Its advantage is that ordinary binary addition handles subtraction with no extra logic: adding a negative number is identical to adding its two's complement bit pattern, so one circuit does both jobs.

Frequently asked questions

1. How do I convert decimal to binary?

Divide by 2 repeatedly and read the remainders bottom to top. 100 gives 1100100. For numbers wider than a byte, go via hex instead - divide by 16 and read up, which is far less error-prone than handling 32 individual bits.

2. Why is 0xFF sometimes 255 and sometimes -1?

Both readings use the same bits. Unsigned 8-bit interprets 11111111 as 255; signed two's complement interprets it as -1, because inverting 00000001 and adding 1 gives 11111111. The type declaration decides, not the data.

3. What is two's complement used for?

The standard way to represent negative numbers in binary, used by essentially every computer. Its advantage is that ordinary binary addition handles subtraction with no extra logic: adding a negative number is identical to adding its two's complement bit pattern, so one circuit does both jobs.

4. What happens when an integer overflows?

The value wraps to the other end of the range: 32,768 in a 16-bit signed integer becomes -32,768. This is a real hazard in embedded code - a 1996 aircraft accident was traced to exactly this, where a 16-bit altitude counter wrapped and the control system read a large negative height.

Related tools