### Decimal, Binary, Octal and Hex

代写lab | math – 这是值得参考的lab代写的题目

#### Introduction

People use decimal numbers but computers work in binary. Programmers use hexadecimal (base

- and octal (base 8) as a convenient representation of binary data. How to convert between these number representations is something that every systems programmer should know. Of course, there are calculators that can convert between decimal and hexadecimal, but systems programmers need to be able to look at hexadecimal data dumps (in particular) and be able to recognise the binary patterns in the data.

This document presents the arithmetic skills of converting between the different numbers bases that programmers use. This material is relevant to Data File Lab, and also to lectures in week 3 of COMP202.

#### Number bases

Decimal is convenient for us because we have ten fingers^1. If we had only four fingers on each hand then we would probably live in an octal world where numbers are made of the digits 0- 7.

##### Decimal

In our decimal number system, the digits of a number have place values. The bottom digit of an integer counts as units, the next digit as tens, then hundreds and so on. When we write 123 what we mean is: 3 units, 2 tens and 1 hundred. It can be written something like the following.

**100 10 1** 1 2 3 The value of each place is a power of ten each place further to the left has a value ten times the place to its right. The value of this number can be computed (in decimal) by multiplying each digit by its place value and adding them up. Working from right to left (because that is easier in other number bases), we see that the value of the number is:

```
3 * 1 + 2 * 10 + 1 * 100
= 3 + 20 + 100
= 123
```

There is nothing surprising here. The number was originally decimal, we broke it down into the individual digits and place values, then recombined it into a decimal number. Our arithmetic would be faulty if the answer was not the same decimal number that we started with!

In the C programming language, numbers are normally written in decimal special notations are used to indicate other number bases.

##### Octal

Other number bases work similarly to decimal, except that the value of each place is a power of the base of the new number system. For example, in an octal world, the values of the digits (from right to left) are 1, 8, 8^2 , 8^3 , and so on. In our decimal number system, 8^2 is 64, and 8^3 is 512. If we write

(^1) Fingers including thumbs.

the octal number 0123 what we mean is 3 units, 2 eights and 1 sixty-four. So the breakdown of the octal number 0 123 looks something like the following.

```
64 8 1
1 2 3
```

The meaning of 0 123 as an octal number is calculated as follows, again working from right to left.

```
3 * 1 + 2 * 8 + 1 * 64
= 64 + 16 + 3
= 83
```

What we have just done is to convert the octal number into decimal by performing the place computation in decimal.

In the C programming language, we indicate that an integer is octal by prefixing the number with a zero digit (as in the example above). Octal numbers can only contain the digits 0-7, because 7 is the maximum place value.

##### Binary

Computers actually work in binary, so the most important number system to understand is binary. In binary, each digit (called a bit) is either 0 or 1, and the place values are powers of two. Because there are so few digits, binary numbers tend to be quite long it takes 10 bits to represent the decimal number 999 and 20 bits to represent 999999 so we will typically work with numbers up to 8 bits in length. Here is one such number: 0b 10110100 . The prefix 0b indicates binary. The breakdown of this binary number into its digits looks like the following.

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

The meaning of this number is 0 units, 0 twos, 1 four, 0 eights, 1 sixteen, 1 thirty-two, 0 sixty-fours, and 1 one-hundred-and-twenty-eight. We can calculate the value as follows. Working from right to left is much easier on binary numbers because you can count off your powers of two as you read off the digits from right to left.

```
0 * 1 + 0 * 2 + 1 * 4 + 0 * 8 + 1 * 16 + 1 * 32 + 0 * 64 + 1 * 128
= 4 + 16 + 32 + 128
= 180
```

The first line of this calculation is actually unnecessary. Since we are multiplying the place values by digits that are either 0 or 1, it is just as easy to write down the simple summation on the second line

- adding together the place values of all the 1 bits in the binary number. Because we are performing our calculations in decimal, the final result is that we have converted from binary to decimal.

```
Powers of 2 are really important in computer hardware, so they are really
important for systems programmers. You should learn the powers of 2 at least
up to 2^10 = 1024: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024. Memorise them.
```

The C programming language has no notation for binary numbers, although gcc has an extension that allows you to use the prefix 0b to represent a binary number, as in the above example.

##### Hexadecimal

The hexadecimal number system is base 16 and it has 16 digits. Since we only have the digits 0- 9 available as digit symbols in our writing, computer scientists use the letters A, B, … F (or their lower-case equivalents a, b, … f) to represent digits ten, eleven, etc up to fifteen. The following table shows the hexadecimal digits and their decimal values.

```
Digit Value Digit Value Digit Value Digit Value
0 0 4 4 8 8 C 12
1 1 5 5 9 9 D 13
2 2 6 6 A 10 E 14
3 3 7 7 B 11 F 15
```

The place values of hexadecimal digits are powers of 16. From right to left, they are units, sixteens, two-hundred-and-fifty-sixes, etc. For example, consider the hexadecimal number 0x25F. The prefix 0x indicates hexadecimal, and the number is interpreted as follows.

```
256 16 1
2 5 F
```

Remembering that F is 15, we can calculate the value of 0x25F as:

```
15 * 1 + 5 * 16 + 2 * 256
= 15 + 80 + 512
= 607
```

The C programming language uses the prefix 0x to indicate a hexadecimal number. You can write a hexadecimal constant anywhere that you would use an integer constant.

```
Exercise : As another example, the hexadecimal number 0x123 has the value
```

- Calculate it and check your answer.

#### Converting Binary to/from Hexadecimal and Octal

Hexadecimal and octal are convenient because their number bases are powers of two. This means that the digits of hexadecimal and octal correspond to groups of binary digits. In particular, each hexadecimal digit is equivalent to 4 binary digits while each octal digit is equivalent to 3 binary digits.

The following tables shows the equivalence between the hexadecimal number 0x25F and the binary number 0b001001011111. Group the binary digits into groups of four as 0x 0010 0101 1111 to make it easier to convert to hexadecimal. These two numbers have the same value.

**Hex** 2 5 F **Bin** 0 0 1 0 0 1 0 1 1 1 1 1

```
Exercise : Check the equivalence of these two numbers by computing their
values using the place values as shown in the following table.
```

**Hex 256 16 1 Hex** 2 5 F **Bin 2048 1024 512 256 128 64 32 16 8 4 2 1 Bin** 0 0 1 0 0 1 0 1 1 1 1 1

Hexadecimal is particularly convenient because each hexadecimal digit represents 4 bits, so two hex digits represent an 8-bit byte. Four hex digits represent two bytes and you can convert a 16 – bit integer from binary to hex by converting each byte separately and then joining them together in the correct sequence. The number of bits in primitive data types is almost always a power of 2 in modern computers, so hexadecimal digits are a good fit.

In general, you can convert a binary number to hexadecimal by grouping the binary bits into groups of 4 (from right to left) and then converting each group of 4 bits into a single hexadecimal digit. For example, here is the conversion of a large binary number.

```
Step Representation
Original binary number 0b^101110110010111101
Groups of four bits^2 001 0 1110 1100 1011 1101^
Decimal value of each group^3 2 14 12 11
Hexadecimal conversion of each group 2 E^ C B D^
```

In contrast to hexadecimal, octal digits each represent three bits. A byte requires three octal digits, but it is not an exact fit and the top digit of an octal representation of a byte is always 0, 1, 2 or 3 because it represents only 2 bits. This means that you cannot easily combine the octal values of two bytes to compute the octal value of a 16-bit short integer. In contrast, the hexadecimal value of two bytes corresponds easily to the individual byte values. However, octal digits are much easier to recognise in patterns of three bits.

Here is an example of converting a byte from binary to octal.

```
Step Representation
Original binary number 0b^10011101
Groups of three bits^4 0 10 011^101
Octal conversion of each group 2 3 5
```

A hexadecimal number is easily converted to binary by expanding each hexadecimal digit into a group of 4 bits. Similarly, octal can be converted to binary by expanding each octal digit into a group of 3 bits. The following examples show the conversion of hexadecimal 0x2ECBD and octal 0 235 to the corresponding binary numbers. You should notice that these examples are the same as the above examples with the steps reversed.

```
Step Representation
Hexadecimal number 2 E C B D^
Decimal value of each digit 2 14 12 11 13^
Four bit conversion of each digit 001 0 1110 1100 1011 1101^
Binary number 00101110110010111101
```

(^2) Note that the left-most group has two leading zeroes added to make it a group of four bits. (^3) When you become familiar with hexadecimal and binary conversion you will learn to recognize the binary patterns corresponding to each hex digit. But, if there is any doubt, mentally convert the 4 binary digits to a decimal number and then convert to hex to be sure. (^4) Note that the left-most group has a leading zero added to make it a group of three bits.

```
Step Representation
Octal number 2 3 5
Three bit conversion of each digit 0 10 011^101
Binary number 010011101
Binary byte drop extra leading 0 bit 10011101
```

#### Converting Decimal to Binary

We have seen above how to convert from binary to decimal by using the values of the bit places. Converting from decimal to binary is a little more difficult because we do our calculations in decimal, and we need to work out the binary digits. The technique that we present here is repeated division by 2. It is worth learning this technique and using it whenever you convert decimal to binary.

```
Warning : Some people convert decimal to binary by guessing the powers of 2.
This is an easy way to make mistakes that are costly. For reliable results, use
the repeated division technique, then you can use the powers of 2 place values
to convert the binary back to decimal and check your calculations.
```

The repeated division technique is as follows.

- Divide the number by 2, writing the quotient below and the remainder (0 or 1) to the right.
- Repeat step 1 until the quotient is zero.
- Read the binary number by reading the remainders from bottom to top.

The following is an example of converting decimal 123 to binary. We write the number, draw a line under it and write the quotient after division by 2 below the line with the remainder to the right. So, 123 divided by 2 is 61 and a remainder of 1; 61 divided by 2 is 30 and a remainder of 1; 30 divided by 2 is 15 and a remainder of zero; and so on until the result of the division is 0.

```
123
61 1
30 1
15 0
7 1
3 1
1 1
0 1
```

Reading the remainders from bottom to top we get the binary number 0b1111011. Using place values, this number has the value 1 + 2 + 8 + 16 + 32 + 64 = 123.

```
Exercise : Convert the decimal number 75 to binary. The answer should be
0b 1001011
```

#### Converting Decimal to Octal and Hexadecimal

The easiest way to convert decimal to octal or hexadecimal is by converting the decimal to binary and then grouping the bits to convert from binary to octal or hexadecimal. This is the recommended approach^5.

```
Exercise : Convert the decimal number 157 to binary, then to octal and
hexadecimal. The octal answer should be 0 235 and the hexadecimal answer is
0x9D.
```

#### Some Well Known Numbers

You should know the powers of 2 up to 2^10 = 1024. They are:

```
2, 4, 8, 16, 32, 64, 128, 256, 512, 1024
```

You should also be familiar some other numbers that occur commonly in computer systems and various application areas of IT.

```
Decimal Number Importance
255 This is the largest unsigned number that fits in a
single byte it is a byte full of 1 bits. In
networking, 255 is commonly found in IPv
subnet masks.
65535 This is the largest unsigned number that fits in 16
bits two bytes full of 1 bits. It is what is meant
by 64K.
```

It is also important to recognise the digit F in hexadecimal as 4 bits that are all 1-bits and the digit 7 in octal as 3 bits that are all 1-bits. The important numbers listed above have very noticeable binary, hexadecimal and octal representations. In fact, the reason that these two numbers are so important is their binary representation.

```
Decimal Binary Hexadecimal Octal
255 0b 1111 1111 0xFF 0377
65535 0b 1111 1111 1111 1111 0xFFFF 0177777
```

We will encounter other significant numbers (and approximations) in the lectures.

Two important properties of binary numbers are helpful to remember:

- If the value is a power of two then the binary representation has exactly one 1-bit in the place representing that power of two. To be more mathematical, the binary representation of the number 2
*n*is a 1-bit followed by*n*0 – bits. - If the value is one less than a power of two then the binary representation is all 1- bits below the place of the power of two. To be more mathematical, the binary representation of the number 2
*n*– 1 is*n*1 – bits.

(^5) Repeated division by 8 can also be used to convert from decimal to octal directly, and repeated division by 16 would convert from decimal to hexadecimal, but division by 16 is probably more difficult for most people than repeatedly dividing by 2.

```
Exercise : Convert 4096 to binary, then to hexadecimal and octal. Check your
answers by converting back. How does this number relate to the important
properties listed above?
Exercise : Convert 511 to binary, then to hexadecimal and octal. Check your
answers by converting back. How does this number relate to the important
properties listed above?
```

#### Negative Numbers

In this lab note we have focussed on unsigned integers. We will examine signed number representations in lectures.