Number Systems: Binary, Decimal, Hex
Speaking the native language of digital circuits
Open interactive version (quiz + challenge)Real-world analogy
What is it?
Number systems are methods of representing numeric values using different bases. Binary (base-2) is the fundamental language of digital circuits using only 0 and 1. Decimal (base-10) is the human-friendly system. Hexadecimal (base-16) is the engineer's shorthand that compresses binary into a compact, readable format used universally in assembly programming, memory addresses, and hardware debugging.
Real-world relevance
When you see a MAC address like 3C:A6:F6:12:E4:8B on your network adapter, those are hexadecimal values. When a blue screen of death shows an error code like 0x0000007E, that is hex. When a color code #D9FE06 defines neon green in CSS, each pair of hex digits represents a binary value for red, green, and blue intensity.
Key points
- Bits Form Patterns — A bit is the smallest unit of data: 0 or 1, off or on, low voltage or high voltage. Eight bits make a byte. With 8 bits, you can represent 256 different values (0 to 255). Every piece of data in a computer -- numbers, text, images -- is ultimately a pattern of bits.
- Bases Represent Values — A number base (or radix) defines how many unique digits are used. Decimal is base-10 (0-9), Binary is base-2 (0-1), Hexadecimal is base-16 (0-9, A-F). The value 255 in decimal = 11111111 in binary = FF in hex. Same quantity, different representation.
- Binary: The Machine's Language — Computers use binary because transistors have two states: on (1) and off (0). Every instruction the 8086 executes, every address it reads, every byte of data is ultimately binary. The instruction MOV AX, 5 is stored in memory as the binary pattern 10111000 00000101 00000000.
- Hex Shortens Binary — Hexadecimal groups every 4 binary digits into one hex digit. This makes long binary strings readable. The 8086 address bus is 20 bits wide -- instead of writing 11111111111111111111, engineers write FFFFFh. Memory dumps, opcodes, and addresses are almost always shown in hex.
- Positional Notation — In any base, each digit position has a weight. In decimal 347: 3x100 + 4x10 + 7x1. In binary 1101: 1x8 + 1x4 + 0x2 + 1x1 = 13. In hex 2F: 2x16 + 15x1 = 47. Understanding positional weights lets you convert between any bases.
- Engineers Read Memory Faster — When debugging 8086 programs, you read memory dumps in hex. Each byte is exactly 2 hex digits. A 16-bit word is 4 hex digits. A 20-bit address is 5 hex digits. This regularity makes hex the universal language of hardware debugging.
- Signed Numbers: Two's Complement — To represent negative numbers, the 8086 uses two's complement. The most significant bit (MSB) is the sign bit: 0 = positive, 1 = negative. For an 8-bit value, 01111111b = +127 and 10000000b = -128. This lets ADD and SUB work for both signed and unsigned numbers with the same circuitry.
- BCD: Binary Coded Decimal — The 8086 supports Binary Coded Decimal where each decimal digit is stored as a 4-bit nibble. The value 59 is stored as 0101 1001 (BCD) instead of 00111011 (pure binary). Instructions like DAA (Decimal Adjust after Addition) correct BCD results.
Code example
; 8086 Assembly: Convert a binary nibble (0-15) to its ASCII hex character
; Input: DL = value 0-15
; Output: DL = ASCII character '0'-'9' or 'A'-'F'
.MODEL SMALL
.STACK 100h
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV DL, 0Bh ; Test value: 11 decimal = 'B' in hex
CMP DL, 09h ; Is it 0-9 or A-F?
JBE IS_DIGIT ; Jump if DL <= 9
ADD DL, 37h ; 0Ah + 37h = 41h = 'A'
JMP PRINT ; 0Bh + 37h = 42h = 'B', etc.
IS_DIGIT:
ADD DL, 30h ; 00h + 30h = 30h = '0'
; 09h + 30h = 39h = '9'
PRINT:
MOV AH, 02h ; DOS print character function
INT 21h ; Print the hex character
MOV AH, 4Ch
INT 21h
MAIN ENDP
END MAINLine-by-line walkthrough
- 1. MOV DL, 0Bh -- We load our test value (11 decimal, which should produce the character 'B') into the DL register.
- 2. CMP DL, 09h -- Compares DL with 9. This determines whether our nibble is a digit (0-9) or a letter (A-F). CMP sets flags without changing DL.
- 3. JBE IS_DIGIT -- 'Jump if Below or Equal.' If DL is 0-9, we jump to the digit handling code. JBE checks the Carry and Zero flags set by CMP.
- 4. ADD DL, 37h -- For values A-F (10-15): adding 37h converts to ASCII. For example, 0Ah (10) + 37h = 41h which is ASCII 'A'. This works because 'A' is at position 41h in the ASCII table.
- 5. ADD DL, 30h -- For values 0-9: adding 30h converts to ASCII. For example, 05h + 30h = 35h which is ASCII '5'. The digit '0' starts at position 30h in ASCII.
- 6. MOV AH, 02h / INT 21h -- DOS function 02h prints the character in DL to the screen. INT 21h invokes the DOS interrupt handler.
Spot the bug
MOV AL, 29h
ADD AL, 18h
DAA
; Expected: BCD 47, Got: something wrongNeed a hint?
Show answer
Explain like I'm 5
Fun fact
Hands-on challenge
More resources
- Number Systems in Computer Science (GeeksforGeeks)
- Binary, Hex, and Octal Explained (YouTube)
- RapidTables Number Converter (RapidTables)
- Two's Complement (Wikipedia)