Chapter 2

Number Systems

Binary, hexadecimal, and data representation in computers

๐Ÿ”ข Why Understand Number Systems?

Computers don't understand numbers like we do. They only work with 0s and 1s (electricity on/off). In RE, you'll see numbers in base 16 (hexadecimal) all the time - in memory addresses, Assembly code, and analysis tools.

๐Ÿ“Š Base 10 (Decimal) - What We Know

๐Ÿ“– Definition: Base (Radix)

The number of different symbols in a number system. Base 10 = 10 digits (0-9).

In the decimal system, each position is worth 10ร— the previous one:

   The number 1234 in decimal:
   
   1    2    3    4
   โ†“    โ†“    โ†“    โ†“
   1ร—10ยณ + 2ร—10ยฒ + 3ร—10ยน + 4ร—10โฐ
   1000  + 200   + 30    + 4     = 1234
            

๐Ÿ’พ Base 2 (Binary) - The Computer's Language

๐Ÿ“– Definition: Binary

A base-2 number system. Only 2 digits: 0 and 1. Each digit is called a Bit.

๐Ÿ“– Definition: Bit

The smallest unit of information in a computer. Can be 0 or 1.

๐Ÿ“– Definition: Byte

A group of 8 bits. Can represent values from 0 to 255.

Converting Binary to Decimal

Each position in binary is worth 2ร— the previous one (right to left):

Binary 11010110 in decimal:

1
128
1
64
0
32
1
16
0
8
1
4
1
2
0
1

128 + 64 + 16 + 4 + 2 = 214

Converting Decimal to Binary

โœ๏ธ Example: Converting 13 to binary

Divide by 2 repeatedly and note the remainder:

13 รท 2 = 6  remainder 1  โ†‘
 6 รท 2 = 3  remainder 0  โ”‚
 3 รท 2 = 1  remainder 1  โ”‚  Read from bottom to top
 1 รท 2 = 0  remainder 1  โ”‚
                     
Result: 13 = 1101 in binary
                

๐Ÿ”ท Base 16 (Hexadecimal) - The Language of RE

๐Ÿ“– Definition: Hexadecimal

A base-16 number system. Uses 16 symbols: 0-9 and A-F.
Common notation: 0x prefix (e.g., 0xFF) or h suffix.

Why is hexadecimal so common in RE?

Important Conversion Table

Decimal Binary Hexadecimal
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
๐Ÿ’ก Quick Conversion Trick

To convert between binary and hex: split binary into groups of 4 and convert each group.

1101 0110 โ†’ D 6 โ†’ 0xD6

โœ๏ธ Example: Memory Address

A typical Windows 32-bit address:

0x00401000

00  40  10  00   (4 bytes)
โ†“   โ†“   โ†“   โ†“
0   64  16  0    (decimal value of each byte)
                

๐Ÿ“ Text Representation - ASCII

๐Ÿ“– Definition: ASCII

A standard for representing characters as numbers. Each character is represented by a number between 0-127.
For example: A = 65, a = 97, 0 = 48.

Char Decimal Hex Char Decimal Hex
A650x41a970x61
B660x42b980x62
Z900x5Az1220x7A
0480x309570x39
Space320x20Enter130x0D
โœ๏ธ Example: The word "Hello" in memory
H    e    l    l    o    (characters)
72   101  108  108  111  (decimal)
0x48 0x65 0x6C 0x6C 0x6F (hex)
                

โž– Negative Numbers

๐Ÿ“– Definition: Two's Complement

A method for representing negative numbers in binary. The leftmost bit (MSB) indicates the sign: 0 = positive, 1 = negative.

In one byte (8 bits):

โœ๏ธ How to Calculate a Negative Number

To get -5:

1. Write 5 in binary:      0000 0101
2. Flip all bits:          1111 1010
3. Add 1:                  1111 1011

1111 1011 = 0xFB = -5 (signed)
                       = 251 (unsigned)
                

๐Ÿ“ Units of Measurement

Unit Size Range (unsigned)
Bit1 bit0-1
Nibble4 bits0-15 (0x0-0xF)
Byte8 bits0-255 (0x00-0xFF)
Word16 bits (2 bytes)0-65,535 (0x0000-0xFFFF)
DWORD32 bits (4 bytes)0-4,294,967,295
QWORD64 bits (8 bytes)0-18.4ร—10ยนโธ
๐Ÿ’ก RE Terminology

DWORD = Double Word (very common in 32-bit)
QWORD = Quad Word (common in 64-bit)
You'll see these terms a lot in IDA and Assembly!

๐Ÿ“‹ Chapter Summary