Labs / Computer Science & Data

Binary / Hex Playground

Visualize bits, bytes, and two's complement representation. Convert between bases and interactively manipulate individual bits.

New to this? There\'s a lesson below ↓
← BACK TO LABS
Interactive Bit View
Configuration
What's happening?

Binary (Base-2) is how computers store data. Each digit is a "bit" that can be 0 or 1.

Hexadecimal (Base-16) is a shorthand for binary. Every 4 bits map exactly to 1 hex digit (0-F).

Two's Complement allows binary numbers to represent negative values. The highest bit (MSB) carries a negative weight.

Try this: Toggle the highest bit in 8-bit Signed mode to jump between positive and negative extremes.

The lesson

Binary, Hexadecimal, and Two's Complement Representation

How unsigned and signed integers are encoded in bits, why hexadecimal maps 1-to-4 with binary, and how two's complement unifies addition and subtraction.

1. Bits, Bytes, and Positional Notation (Base 2)

Digital hardware stores all information as bits (binary digits, 0 or 1). A group of 8 bits is a byte, and a group of 4 bits is a nibble.

In decimal (base 10), each digit position represents a power of 10 (10⁰ = 1, 10¹ = 10, 10² = 100). In binary (base 2), each bit position i (counting from 0 on the right, the Least Significant Bit / LSB, to n − 1 on the left, the Most Significant Bit / MSB) has weight 2ⁱ:

Bit index i:     7    6    5    4    3    2    1    0
Weight 2ⁱ:     128   64   32   16    8    4    2    1
Bits:            0    0    1    0    1    1    0    1
Value:                    32       + 8  + 4     + 1  =  45

An n-bit unsigned integer adds up all active weights and can represent values from 0 to 2ⁿ − 1:

  • 8-bit unsigned: 0 to 2⁸ − 1 = 255
  • 16-bit unsigned: 0 to 2¹⁶ − 1 = 65,535
  • 32-bit unsigned: 0 to 2³² − 1 = 4,294,967,295

2. Hexadecimal (Base 16)

Reading 32-bit binary strings is error-prone, so systems programming uses hexadecimal (base 16), written with the prefix 0x and digits 0–9 and A–F (where A = 10, B = 11, C = 12, D = 13, E = 14, F = 15).

Because 16 = 2⁴, one hex digit maps to exactly 4 bits (one nibble). To convert between binary and hex, group the bits by fours from right to left and convert each 4-bit group independently:

Binary (grouped by 4):   1101   0110   1010   1111
Decimal of each group:    13      6     10     15
Hexadecimal:               D      6      A      F   →  0xD6AF

Type 0xFF into the Hexadecimal input in the simulator above: two F digits immediately light up two 4-bit groups of ones (1111 1111 = 255).

3. Signed Integers and Two's Complement

Set the simulator to 8-bit, Unsigned, and enter 251 (1111 1011). Now switch Representation to Signed (Two's Complement). Without changing a single bit, the decimal value becomes −5.

How does two's complement represent negative numbers? Every bit keeps its positive weight 2ⁱ, except the Most Significant Bit (MSB), which has a negative weight −2ⁿ⁻¹ (−128 in 8-bit):

Weight:   -128   64   32   16    8    4    2    1
Bits:        1    1    1    1    1    0    1    1
Value:    -128 + 64 + 32 + 16  + 8  + 0  + 2  + 1  =  -128 + 123  =  -5

To negate any two's complement number x: invert all bits (~x) and add 1:

+5  =  0000 0101
~5  =  1111 1010   (bitwise NOT)
+1  =  1111 1011   (-5 in two's complement)

Why every modern CPU uses two's complement:

  • There is only a single representation of zero (0000 0000), unlike older sign-magnitude or ones' complement systems that had both +0 and −0.
  • Unified addition hardware: The CPU's binary adder circuit produces the exact same bit pattern whether the operands are interpreted as signed or unsigned.
  • The range of an n-bit signed integer is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1 (for 8-bit: −128 to +127). Notice −1 is always all ones (1111 1111).

4. Bitwise Operations and Integer Overflow

OperationSymbolBehavior & Mathematical Effect
Bitwise NOT~xInverts every bit (0 ↔ 1). In two's complement, ~x = −x − 1.
Left Shiftx << kShifts bits left by k positions, filling with 0 on the right. Multiplies by 2ᵏ (if no bits overflow).
Right Shiftx >> kShifts bits right by k positions, dividing by 2ᵏ (rounding down). Logical shift fills with 0; arithmetic shift copies the sign bit.

Overflow: When an arithmetic result exceeds the fixed bit width, the high bits are discarded. In 8-bit unsigned, 255 + 1 wraps around to 0. In 8-bit signed, adding 1 to the maximum positive value 127 (0111 1111) produces 1000 0000, which wraps around to −128.

Exercises

1. Convert decimal 200 to 8-bit binary and hexadecimal by hand, then verify in the simulator.

Show solution
200 = 128 + 64 + 8 → binary 1100 1000. Grouping by 4 bits: 1100 = 12 (C) and 1000 = 8 → 0xC8.

2. What decimal value does the 8-bit pattern 1100 1000 (0xC8) represent in Signed (Two's Complement) mode?

Show solution
With MSB weight −128: −128 + 64 + 8 = −56 (or 200 − 256 = −56).

Quiz

What is the hexadecimal representation of the 8-bit binary number 0011 1111?

In 8-bit two's complement (signed), what decimal value does 1000 0001 represent?

What is the range of a signed 8-bit two's complement integer?

Why do CPUs use two's complement instead of sign-magnitude for signed integers?