In this lessons you'll learn about three number systems: Binary (base-2), Decimal (base-10), and Hexadecimal (base-16).
Binary is used by computers because it represents data using only 0s and 1s. Decimal is the system we use daily, while hexadecimal is often used in programming to represent large binary numbers compactly.
Read the following examples:
Let's take a look at binary numbers, which are the foundation of how computers store and process data. Binary numbers use only two digits: 0 and 1. This is called base-2.
Each digit in a binary number is called a 'bit'. The position of each bit represents a power of 2, starting from 20 on the far right. Here's an example:
Binary Number | Position (Power of 2) | Value |
---|---|---|
1 | 23 | 8 |
0 | 22 | 0 |
1 | 21 | 2 |
0 | 20 | 0 |
Adding these values together gives: 8 + 0 + 2 + 0 = 10 (in decimal).
Binary numbers are used because computers operate using electrical signals, which can either be on (1) or off (0). This makes binary the most efficient way for computers to represent data.
Now that you understand binary numbers, you're ready to explore how to convert between binary and other number systems!
In this step, you'll learn about decimal numbers, which are the number system we use in our everyday lives. Decimal numbers are based on base-10, meaning they use ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Each digit in a decimal number has a position, and each position represents a power of 10, starting from 100 on the far right. Here's an example:
Decimal Number | Position (Power of 10) | Value |
---|---|---|
3 | 102 | 300 |
4 | 101 | 40 |
5 | 100 | 5 |
Adding these values together gives: 300 + 40 + 5 = 345.
Decimal numbers are widely used because they are intuitive and align with the way humans have historically counted using ten fingers. However, computers use binary numbers instead, as they are more efficient for electronic systems. Understanding decimal numbers is essential for converting between different number systems, which you'll explore in this lesson!
Next let's learn about hexadecimal numbers, which are based on base-16. Hexadecimal numbers use sixteen symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. The letters A to F represent the decimal values 10 to 15.
Each digit in a hexadecimal number has a position, and each position represents a power of 16, starting from 160 on the far right. Here's an example:
Hexadecimal Number | Position (Power of 16) | Value |
---|---|---|
1 | 162 | 256 |
A | 161 | 160 |
F | 160 | 15 |
Adding these values together gives: 256 + 160 + 15 = 431 (in decimal).
Hexadecimal numbers are widely used in computing because they provide a more compact way to represent binary numbers. For example, a single hexadecimal digit can represent four binary digits (bits). This makes hexadecimal especially useful for representing memory addresses, colours in web design, and other binary data in a human-readable format.
Now that you understand hexadecimal numbers, you're ready to explore how to convert between hexadecimal and other number systems!
In this step, you'll write Python code to convert a decimal number to binary.
Add the following code:
# Function to convert decimal to binary
def decimal_to_binary(decimal_number):
return bin(decimal_number).replace("0b", "")
# Test the function
decimal_number = 10
binary_result = decimal_to_binary(decimal_number)
print(f"The binary representation of {decimal_number} is {binary_result}")
Run your code. You should see the binary representation of the decimal number 10. Try changing the value of decimal_number
to test with other numbers!