A binary digit or bit is a
0
or a
1
.
Thus a bit has one of two possible values.
A series of 8 bits is called a byte.
For example, 01010101
.
A series of 4 bits is called a nibble.
For example, 0101
.
A computer’s memory is a series of containers called cells. Each cell holds one byte.
Each cell has an identifying number, called its
memory address.
On our machine
storm.cis.fordham.edu
,
a memory address is conventionally written in hexadecimal notation.
That’s why the number 9
in the following diagram is followed by
A
and
B
instead of by
10
and
11
.
On our machine,
a C++ value of data type int
occupies 32 bits of memory,
so we use four consecutive cells to hold it.
For example, the memory in the diagram
uses cells number 4 through 7 inclusive
to hold the 32-bit int
value
2025
.
int i {2025};
Let’s see how to write a number in binary (base 2) and hexadecimal (base 16).
Each place has 10 times the value of the previous one.
Depending on the number,
we might need to write ten decial digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
1000’s place | 100’s place | 10’s place | 1’s place |
---|---|---|---|
2 | 0 | 2 | 5 |
2 × 1000 = 2000 0 × 100 = 0 2 × 10 = 20 5 × 1 = 5 2025
Each place has 2 times the value of the previous one.
The only binary digits we will ever need to write are 0 and 1.
1024’s place | 512’s place | 256’s place | 128’s place | 64’s place | 32’s place | 16’s place | 8’s place | 4’s place | 2’s place | 1’s place |
---|---|---|---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
1 × 1024 = 1024 1 × 512 = 512 1 × 256 = 256 1 × 128 = 128 1 × 64 = 64 1 × 32 = 32 0 × 16 = 0 1 × 8 = 8 0 × 4 = 0 0 × 2 = 0 1 × 1 = 1 2025
Our number 2025 consists of a series of 11 bits when written in binary.
We will usually consider bits in groups of 4 or 8 at a time,
so let’s expand our 11-bit number to 16 bits
by adding 5 leading 0’s.
Now our number 2025 consists of 2 complete bytes, because 2 = 16/8.
32768’s place | 16384’s place | 8192’s place | 4096’s place | 2048’s place | 1024’s place | 512’s place | 256’s place | 128’s place | 64’s place | 32’s place | 16’s place | 8’s place | 4’s place | 2’s place | 1’s place |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
0 × 32768 = 0 0 × 16384 = 0 0 × 8192 = 0 0 × 4096 = 0 0 × 2048 = 0 1 × 1024 = 1024 1 × 512 = 512 1 × 256 = 256 1 × 128 = 128 1 × 64 = 64 1 × 32 = 32 0 × 16 = 0 1 × 8 = 8 0 × 4 = 0 0 × 2 = 0 1 × 1 = 1 2025
As we just saw, writing the value of a number in binary usually takes many bits. That’s why they invented hexadecimal digits. Each hexadecimal digit (hex digit) stands for a series of 4 binary digits (bits). In other words, each hex digit stands for 1 nibble.
Here are the 16 nibbles that the 16 hex digits stand for.
hex digit | nibble (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 |
We can now write out 16-bit number
(2025 = 0000011111101001
)
with only 4 hex digits (07E9
):
32768’s place | 16384’s place | 8192’s place | 4096’s place | 2048’s place | 1024’s place | 512’s place | 256’s place | 128’s place | 64’s place | 32’s place | 16’s place | 8’s place | 4’s place | 2’s place | 1’s place | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
binary | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
hex | 0 | 7 | E | 9 |
Type control-d
into the first two programs
to signal that you’re done typing.
dectohex.C
:
convert base 10 to base 16.
hextodec.C
:
convert base 16 to base 10.
table.C
:
decimal (base 10) and hexadecimal (base 16), side by side.