Convert between positive hexadecimal and positive binary
Data Representation: Hexadecimal ↔️ Binary Conversion
Why Hexadecimal Matters
Think of hexadecimal (base‑16) as a shortcut for binary (base‑2). Each hex digit represents exactly four binary bits, so you can write large binary numbers much more compactly. It’s like using words instead of single letters to describe a sentence.
Analogy: The 4‑Bit Backpack
Imagine a backpack that can hold 16 different items (0–15). Each item is a hex digit. Inside the backpack, you have four smaller pockets (binary bits) that can be either open (1) or closed (0). The combination of the four pockets tells you exactly which item is in the backpack.
Step‑by‑Step Conversion
- Write the hexadecimal number with a
0xprefix to remind you it’s hex. - Convert each hex digit to its 4‑bit binary equivalent using the table below.
- Remove any leading zeros from the most significant group (unless the number is zero).
- Group the binary digits into sets of four for readability (optional).
Hexadecimal to Binary Reference Table
| Hex | Binary (4 bits) |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
| 8 | 1000 |
| 9 | 1001 |
| A | 1010 |
| B | 1011 |
| C | 1100 |
| D | 1101 |
| E | 1110 |
| F | 1111 |
Example: Convert 0x1A3F to Binary
1️⃣ 0x1A3F → 1 A 3 F
2️⃣ Look up each digit:
1 → 0001, A → 1010, 3 → 0011, F → 1111
3️⃣ Concatenate: 0001 1010 0011 1111
4️⃣ Remove leading zeros if desired (here we keep them for clarity). Final binary: 0001101000111111 (or grouped as 0001 1010 0011 1111).
Practice Problems
- Convert
0x4B2to binary. - Convert
0xFF00to binary. - Convert
0x0C3to binary. - Convert the binary number
11001101to hexadecimal.
Quick Check: Binary to Hex
To reverse the process, group the binary number into sets of four bits from the right, pad the leftmost group with zeros if necessary, then look up each group in the table.
Key Takeaway
Remember: 1 hex digit = 4 binary bits. Use the table as your cheat sheet, and practice converting back and forth to build muscle memory. Happy coding! 🚀
Revision
Log in to practice.