Ch 1. Number Systems

1.2 Representations of Numbers in Computers: Binary, Hexadecimal

When you are using a computer, you are using a digital machine, and these machines operate using electrical signals that can be in two states: off or on. If we assign these two states numbers, such as 0 for off and 1 for on, then we can use the binary number system (base 2) to represent these signals. Long binary numbers can be challenging for humans to read, especially in programming and hardware design. For this reason, hexadecimal numbers (base 16) are used to make the binary numbers more compact.

Understanding how numbers are represented in binary and hexadecimal is essential for interpreting memory addresses, machine code, colour codes in web design, and low-level data structures.

Unlike the decimal system you are familiar with, which has digits 0 through 9, the binary system only has digits 0 and 1. These binary digits are called bits. Binary numbers are used internally by almost all modern computers and digital systems.

Each position in a binary number represents a power of 2, starting from the right:

[latex]1101_2 = 1 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 8 + 4 + 0 + 1 = 13_{10}[/latex]

 

Example 1.6

The binary number 1010 represents the decimal number 10. This is because binary is a base-2 number system, meaning each digit (bit) represents a power of 2, starting from the rightmost digit (least significant bit).

Binary:     1    0    1    0

Position:   3    2    1    0

Value:    2^3  2^2  2^1  2^0

    8 +  0 +  2 +  0 = 10

So,

[latex]1010_2 = 1 \cdot 2^3 + 0 \cdot 2^2 + 1 \cdot 2^1 + 0 \cdot 2^0 = 8 + 0 + 2 + 0 = 10_{10}[/latex]

Digital computing is based on binary numbers, with every number, character, or instruction being able to be represented in binary. If you consider the decimal number 10, in 8-bit binary it would be stored in memory as 00001010. This memory address could be used for a loop count, a line feed in ASCII, or the intensity of a pixel to display.

Think of binary like a light switch system where each switch (bit) is either ON (1) or OFF (0). The combination of ON/OFF states determines the total value, just as flipping switches adds up to a specific number.

Binary is used to represent instructions in machine code, Boolean logic (true/false), and on/off states in circuits.

The hexadecimal system uses 16 digits: 0 to 9 and A to F, where A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15. Each hexadecimal digit represents four binary digits, making it a compact way to represent binary data.

Example 1.7

The hexadecimal number [latex]2F_{16}[/latex] represents the decimal number 47. Hexadecimal is a base-16 number system, which means each digit represents a power of 16. In [latex]2F_{16}[/latex], the digit 2 is in the [latex]16^1[/latex] (or “sixteens”) place, and F (which equals 15) is in the [latex]16^0[/latex] (or “ones”) place.

So, we compute

[latex]2F_{16} = 2 \cdot 16^1 + 15 \cdot 16^0 = 2 \cdot 16 + 15 \cdot 1 = 32 + 15 = 47_{10}[/latex]

Hexadecimal is widely used in computing because it provides a compact and human-readable way to represent binary values. Each hex digit corresponds to exactly four binary digits, making it easy to convert between the two.

The binary equivalent is 2 = 0010, F = 1111, which gives [latex]2F_{16} = 00101111_2[/latex].

In web development, colours are often represented in hexadecimal. For example, the colour #2F4F4F is a shade of dark slate gray. In memory addresses, 0x2F might refer to a specific byte in RAM or a register in a microcontroller.

 

Hexadecimal is commonly used in memory addresses (e.g., 0xFF), colour codes in web design (e.g., #FF5733), and machine-level debugging and assembly language.

It is difficult for humans to understand binary, while it is the language computers best understand. That is why hexadecimal is used as a more human-friendly shorthand. Both systems are essential for understanding how data is stored, processed, and transmitted in digital systems.

 

Real-World Example 1.2: Digital Colour in Web Design

You have seen how ASCII assigns decimal values to alphanumeric characters, and in web development a similar process assigns hexadecimal values to colours. Colours are defined by combinations of red, green, and blue (RGB) components, with each component being stored as an 8-bit binary number (0 to 255 in decimal). To make them more human-readable, they are then converted to hexadecimal.

Suppose we have a calm, medium blue colour which has the colour code #3A7BD5 when used in HTML and CSS (Cascading Style Sheets). If we extract the RGB components from this colours, we have

Red: [latex]3A_{16} = 00111010_2[/latex] = 58

Green: [latex]7B_{16} = 01111011_2[/latex] = 123

Blue: [latex]D5_{16} = 11010101_2[/latex] = 213

Each of these values is stored in binary in the computer’s memory but displayed in hexadecimal for human readability.

Designers use hex codes to precisely control colour schemes. Browsers interpret these codes by converting them back to binary and rendering the appropriate colour on screen. This is a practical example of how binary and hexadecimal systems are used together in real-world computing.

The full 24-bit colour space (8 bits per RGB component) allows for 224 = 16,777,216 unique colours.