Convert the following hexadecimal numbers into denary: 将下列十六进制数转换为二进制数:
a 6 B
f A 01
b 9 C
g BB B 4 g BB B 4
c 4 A
h CC A 8 h CC A 8
d F F
i 12 A E
e 1 F F
j A D 89
To convert from denary to hexadecimal involves successive division by 16 until the value " 0 " is reached. This is best shown by two examples: 要从二进制转换为十六进制,需要连续除以 16,直到取值为 "0"。两个例子最能说明这一点:
?? Example 1 ?? 示例 1
Convert the denary number, 2004, into hexadecimal. 将二进制数 2004 转换为十六进制数。
This method involves successive division by 16 until the value 0 is reached. We start by dividing the number 2004 by 16 . The result of the division including the remainder (even if it is 0 ) is written under 2004 and then further divisions by 16 are carried out (that is, 2004-:16=1252004 \div 16=125 remainder 4;125-:16=74 ; 125 \div 16=7 remainder 13;7-:16=013 ; 7 \div 16=0 remainder 7 ). The hexadecimal number is obtained from the remainders written in reverse order: 这种方法是连续除以 16,直到数值为 0。我们先用数字 2004 除以 16。包括余数在内的除法结果(即使是 0)写在 2004 下面,然后再除以 16(即 2004-:16=1252004 \div 16=125 余数 4;125-:16=74 ; 125 \div 16=7 余数 13;7-:16=013 ; 7 \div 16=0 余数 7)。十六进制数由按相反顺序写入的余数得出:
Figure 1.2a 图 1.2a
2 Example 2 2 示例 2
Convert the denary number, 8463 , into hexadecimal. 将二进制数 8463 转换为十六进制数。
We start by dividing the number 8463 by 16 . The result of the division including the remainder (even if it is 0 ) is written under 8463 and then further divisions by 16 are carried out (that is, 8463-:16=5288463 \div 16=528 remainder 15; 528-:16=33528 \div 16=33 remainder 0;33-:16=20 ; 33 \div 16=2 remainder 1;2-:16=01 ; 2 \div 16=0 remainder 2 ). The hexadecimal number is obtained from the remainders written in reverse order: 我们先用 8463 除以 16。包括余数(即使是 0)在内的除法结果写在 8463 下面,然后再除以 16(即 8463-:16=5288463 \div 16=528 余数 15; 528-:16=33528 \div 16=33 余数 0;33-:16=20 ; 33 \div 16=2 余数 1;2-:16=01 ; 2 \div 16=0 余数 2)。十六进制数由按相反顺序写入的余数得出:
read the remainder from bottom to top
to get the hexadecimal number:
2,1,0,F,(F=15)| read the remainder from bottom to top |
| :--- |
| to get the hexadecimal number: |
| 2 1 0 $F$ $(F=15)$ |
16
528
remainder: 其余部分
15
16
33
remainder: 其余部分
0
16
2
remainder: 其余部分
1
0
remainder: 其余部分
2
16 8463 Delta "read the remainder from bottom to top
to get the hexadecimal number:
2,1,0,F,(F=15)"
16 528 remainder: 15
16 33 remainder: 0
16 2 remainder: 1
0 remainder: 2 | 16 | 8463 | | | $\Delta$ | read the remainder from bottom to top <br> to get the hexadecimal number: <br> 2 1 0 $F$ $(F=15)$ |
| :---: | :---: | :---: | :---: | :---: | :---: |
| 16 | 528 | remainder: | 15 | | |
| 16 | 33 | remainder: | 0 | | |
| 16 | 2 | remainder: | 1 | | |
| | 0 | remainder: | 2 | | |
Figure 1.2b 图 1.2b
Activity 1.6 活动 1.6
Convert the following denary numbers into hexadecimal: 将下列二进制数转换为十六进制数:
a
9
8
f
1
0
0
0
b
2
2
7
g
2
6
3
4
c
4
9
0
h
3
7
4
3
d
5
1
1
i
4
0
0
7
e
8
2
6
j
5
0
0
0
a 9 8 f 1 0 0 0
b 2 2 7 g 2 6 3 4
c 4 9 0 h 3 7 4 3
d 5 1 1 i 4 0 0 7
e 8 2 6 j 5 0 0 0| a | 9 | 8 | f | 1 | 0 | 0 | 0 | |
| :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |
| b | 2 | 2 | 7 | g | 2 | 6 | 3 | 4 |
| c | 4 | 9 | 0 | h | 3 | 7 | 4 | 3 |
| d | 5 | 1 | 1 | i | 4 | 0 | 0 | 7 |
| e | 8 | 2 | 6 | j | 5 | 0 | 0 | 0 |
1.1.3 Use of the hexadecimal system 1.1.3 十六进制系统的使用
As we have seen, a computer can only work with binary data. Whilst computer scientists can work with binary, they find hexadecimal to be more convenient to use. This is because one hex digit represents four binary digits. A complex binary number, such as 1101001010101111 can be written in hex as D2AF. The hex number is far easier for humans to remember, copy and work with. This section reviews four uses of the hexadecimal system: 正如我们所见,计算机只能处理二进制数据。虽然计算机科学家可以处理二进制数据,但他们认为使用十六进制更为方便。这是因为一个十六进制数代表四个二进制数。一个复杂的二进制数,如 11010010101111,可以用十六进制写成 D2AF。对于人类来说,十六进制数更容易记忆、复制和使用。本节将介绍十六进制系统的四种用途:
》 error codes 错误代码
》 MAC addresses MAC地址
» IPv6 addresses "IPv6 地址
»HTML colour codes "HTML 颜色代码
The information in this section gives the reader sufficient grounding in each topic at this level. Further material can be found by searching the internet, but be careful that you don’t go off at a tangent. 本节中的信息为读者提供了本级别每个主题的足够基础。更多资料可在互联网上搜索,但要注意不要跑题。
Error codes 错误代码
Error codes are often shown as hexadecimal values. These numbers refer to the memory location of the error and are usually automatically generated by the computer. The programmer needs to know how to interpret the hexadecimal error codes. Examples of error codes from a Windows system are shown below: 错误代码通常显示为十六进制值。这些数字指的是发生错误的内存位置,通常由计算机自动生成。程序员需要知道如何解释十六进制错误代码。Windows 系统的错误代码示例如下:
Find out more 了解更多
Another method used to trace errors during program development is to use memory dumps, where the memory contents are printed out either on screen or using a printer. Find examples of memory dumps and find out why these are a very useful tool for program developers. 另一种在程序开发过程中跟踪错误的方法是使用内存转储,即在屏幕上或使用打印机打印出内存内容。查找内存转储的示例,了解为什么内存转储是程序开发人员非常有用的工具。
Figure 1.3 Example of error codes 图 1.3 错误代码示例
Media Access Control (MAC) addresses 媒体访问控制 (MAC) 地址
Media Access Control (MAC) address refers to a number which uniquely identifies a device on a network. The MAC address refers to the network interface card (NIC) which is part of the device. The MAC address is rarely changed so that a particular device can always be identified no matter where it is. 介质访问控制 (MAC) 地址是指唯一标识网络设备的一个数字。MAC 地址指的是作为设备一部分的网络接口卡(NIC)。MAC 地址很少更改,因此无论在哪里都能识别特定设备。
A MAC address is usually made up of 48 bits which are shown as 6 groups of two hexadecimal digits (although 64-bit addresses also exist): MAC 地址通常由 48 位组成,显示为 6 组两个十六进制数字(也有 64 位地址):
NN-NN-NN-DD-DD-DDN N-N N-N N-D D-D D-D D
or 或
NN:NN:NN:DD:DD:DD
where the first half (NN-NN-NN)(N N-N N-N N) is the identity number of the manufacturer of 其中, (NN-NN-NN)(N N-N N-N N) 的前半部分是该产品制造商的标识号。
the device and the second half ( DD-DD-DD)D D-D D-D D) is the serial number of the device. 后半部分( DD-DD-DD)D D-D D-D D) 是设备的序列号。
where the first half (NN-NN-NN)(N N-N N-N N) is the identity number of the manufacturer of 其中, (NN-NN-NN)(N N-N N-N N) 的前半部分是该产品制造商的标识号。
the device and the second half (DD-DD-DD)(D D-D D-D D) is the serial number of the device. For example: 后半部分 (DD-DD-DD)(D D-D D-D D) 是设备的序列号。例如 00-1C-B3-4F-25-FE00-1 \mathrm{C}-\mathrm{B3}-4 \mathrm{~F}-25-\mathrm{FE} is the MAC address of a device produced by the Apple Corporation (code: 001CB3) with a serial number of: 4F25FE. Very often lowercase hexadecimal letters are used in the MAC address: 00-1c-b3-4f-25-fe. Other manufacturer identification numbers include: 00-1C-B3-4F-25-FE00-1 \mathrm{C}-\mathrm{B3}-4 \mathrm{~F}-25-\mathrm{FE} 是 Apple 公司生产的设备(代码:001CB3)的 MAC 地址,序列号为 4F25FE:4F25FE。MAC 地址中通常使用小写十六进制字母:00-1C-B3-4F-25-FE。其他制造商标识号包括 00-14-2200-14-22 which identifies devices made by Dell 00-14-2200-14-22 可识别戴尔制造的设备 00-4000-40 - 96 which identifies devices made by Cisco 00-4000-40 - 96,用于识别 Cisco 制造的设备
Find out more 了解更多
Try to find the MAC addresses of some of your own devices le.g. mobile phone and tablet) and those found in the school. 尝试查找您自己的一些设备(如手机和平板电脑)和学校里的设备的 MAC 地址。
Link 链接
Refer to Chapter 3 for more detail on IP addresses. 有关 IP 地址的更多详情,请参阅第 3 章。
Find out more 了解更多
Try to find the IPv4 and IPv6 addresses of some of your own 尝试查找自己的一些 IPv4 和 IPv6 地址
devices (e.g. mobile of some of your own 设备(例如您自己的一些移动
devices le.g. mobile phone and tablet) and those found in the school. 手机和平板电脑等设备)和学校内的设备。
Link 链接
Refer to Chapter 3 for more detail on MAC addresses. ◻\square 有关 MAC 地址的更多详情,请参阅第 3 章。 ◻\square
00 - a0 - c9 which identifies devices made by Intel, and so on. 00 - a0 - c9 表示英特尔公司生产的设备,以此类推。
Internet Protocol (IP) addresses 互联网协议(IP)地址
Each device connected to a network is given an address known as the Internet Protocol (IP) address. An IPv4 address is a 32-bit number written in denary or hexadecimal form: e.g. 109.108.158.1 (or 77.76.9e. 01 in hex). IPv4 has recently been improved upon by the adoption of IPv6. An IPv6 address is a 128-bit number broken down into 16-bit chunks, represented by a hexadecimal number. For example: 每个连接到网络的设备都有一个地址,即互联网协议(IP)地址。IPv4 地址是以十进制或十六进制形式书写的 32 位数字:如 109.108.158.1(或十六进制 77.76.9e.01)。IPv4 最近通过采用 IPv6 得到了改进。IPv6 地址是一个 128 位的数字,分成 16 位的小块,用十六进制数字表示。例如
a8fb:7a88:fff0:0fff:3d21:2085:66fb:f0fa
Note IPv6 uses a colon (😃 rather than a decimal point (.) as used in IPv4. 注意 IPv6 使用冒号 (😃),而不是 IPv4 中使用的小数点 (.)。
HyperText Mark-up Language (HTML) colour codes 超文本标记语言(HTML)颜色代码
HyperText Mark-up Language (HTML) is used when writing and developing web pages. HTML isn’t a programming language but is simply a mark-up language. A mark-up language is used in the processing, definition and presentation of text (for example, specifying the colour of the text). 超文本标记语言(HTML)用于编写和开发网页。HTML 并非编程语言,而只是一种标记语言。标记语言用于处理、定义和呈现文本(例如,指定文本的颜色)。
HTML uses <tags> which are used to bracket a piece of text for example, <h1> and </h1> surround a top-level heading. Whatever is between the two tags has been defined as heading level 1 . Here is a short example of HTML code: HTML 使用 <tags> 来括弧括住一段文本,例如,<h1> 和 </h1> 包围着一个顶级标题。这两个标签之间的内容被定义为一级标题。下面是一个简短的 HTML 代码示例:
<h1 style=“Color:#FF0000;”>This is a red heading</h1> <h1 style="Color:#FF0000;">这是一个红色标题</h1>
<h2 style=“color:#00FF00;”>This is a green heading</h2> <h2 style="color:#00FF00;">这是一个绿色标题</h2>
<h3 style=“color:#0000FF;”>This is a blue heading</h3> <h3 style="color:#0000FF;">T 这是一个蓝色标题</h3>
Figure 1.4 图 1.4
HTML is often used to represent colours of text on the computer screen. All colours can be made up of different combinations of the three primary colours (red, green and blue). The different intensity of each colour (red, green and blue) is determined by its hexadecimal value. This means different hexadecimal values represent different colours. For example: HTML 常用来表示计算机屏幕上文本的颜色。所有颜色都可以由三原色(红、绿、蓝)的不同组合构成。每种颜色(红、绿、蓝)的不同强度由其十六进制值决定。这意味着不同的十六进制值代表不同的颜色。例如
»# FF 0000 represents primary colour red "# FF 0000 代表红色原色
》# 00 FF 00 represents primary colour green 》# 00 FF 00 代表绿色原色
》# 0000 FF represents primary colour blue 0000 FF 代表蓝色原色
》# FF 00 FF represents fuchsia 》# FF 00 FF 代表紫红色
》# FF 8000 represents orange 》# FF 8000 代表橙色
》# B1 8904 represents a tan colour, B1 8904 代表棕褐色、
and so on producing almost any colour the user wants. The following diagrams show the various colours that can be selected by altering the hex ‘intensity’ of red, green and blue primary colours. The colour ‘FF9966’ has been chosen as an example: 以此类推,几乎可以产生用户想要的任何颜色。下图显示了通过改变红、绿、蓝三原色的十六进制 "强度 "可以选择的各种颜色。我们选择了 "FF9966 "作为示例:
FF9966
A Figure 1.5 Examples of HTML hex colour codes 图 1.5 HTML 十六进制颜色代码示例
The # symbol always precedes hexadecimal values in HTML code. The colour codes are always six hexadecimal digits representing the red, green and blue components. There are a possible 256 values for red, 256 values for green and 256 values for blue giving a total of 256 xx256 xx256256 \times 256 \times 256 (i.e. 16777216 ) possible colours. 在 HTML 代码中,十六进制值之前总是有 # 符号。颜色代码总是由六个十六进制数字组成,分别代表红色、绿色和蓝色。红色有 256 个值,绿色有 256 个值,蓝色有 256 个值,因此总共有 256 xx256 xx256256 \times 256 \times 256 (即 16777216)种可能的颜色。
Activity 1.7 活动 1.7
1 Using software on your computer (for example, text colour option in Word), find out what colours would be represented by the following RGB denary value combinations: 1 使用计算机上的软件(例如 Word 中的文本颜色选项),找出下列 RGB 代数值组合可以代表什么颜色:
a Red 53 a 红色 53
Green 55 绿色 55
b Red b 红色
201
Blue 139 蓝色 139
Green 绿色
122
Blue 蓝色
204
c Red c 红色
12
Green 绿色
2 Convert each of the above denary numbers into hexadecimal. 2 将上述每个二进制数转换成十六进制数。
1.1.4 Addition of binary numbers 1.1.4 二进制数的加法
This section will look at the addition of two 8-bit positive binary numbers. 本节将介绍两个 8 位二进制正数的加法运算。
Note the following key facts when carrying out addition of two binary digits: 在进行两位二进制数的加法运算时,请注意以下关键事实:
For comparison: if we add 7 and 9 in denary the result is: carry =1=1 and sum =6=6; if we add 7,9 and 8 the result is: carry =2=2 and sum =4=4, and so on. 比较一下:如果我们把 7 和 9 相加,结果是:进位 =1=1 ,和 =6=6 ;如果我们把 7、9 和 8 相加,结果是:进位 =2=2 ,和 =4=4 ,以此类推。
Advice 建议
Here’s a quick recap on the role of carry and sum. If we want to add the numbers 97 and 64 in decimal, we: 下面简要回顾一下进位和和的作用。如果我们想把十进制数 97 和 64 相加,我们应该
add the numbers in the right hand column first 先将右栏的数字相加
if the sum is greater than 9 then we carry a value to the next column 如果总和大于 9,则将一个值转入下一列
we continue moving left, adding any carry values to each column until we are finished. 我们继续向左移动,在每一列中添加任何携带值,直到完成为止。
Adding in binary follows the same rules except that we carry whenever the sum is greater than 1. 二进制加法也遵循同样的规则,只是只要总和大于 1,我们就进行运算。
? Example 1 ?示例 1
Add 00100111+0100101000100111+01001010 添加 00100111+0100101000100111+01001010
We will set this out showing carry and sum values: 我们将显示进位值和和值:
? Example 2 ?示例 2
a Convert 126 and 62 into binary. a 将 126 和 62 转换成二进制。
b Add the two binary values in part a and check the result matches the addition of the two denary numbers b 将 a 部分中的两个二进制值相加,并检查结果是否与两个二进制数相加的结果一致
10111100 has the equivalent denary value of 128+32+16+8+4=188128+32+16+8+4=188 which is the same as 126+62126+62. 10111100 的对应面值为 128+32+16+8+4=188128+32+16+8+4=188 ,与 126+62126+62 相同。
Activity 1.8 活动 1.8
Carry out the following binary additions: 进行以下二进制加法运算:
a
00011101+0110011000011101+01100110
ff
00111100+0111101100111100+01111011
b
00100111+0011111100100111+00111111
g
00111111+0011111100111111+00111111
c
00101110+0100110100101110+01001101
h
00110001+0011111100110001+00111111
d
01110111+0011111101110111+00111111
i.
01111111+01111101111111+011111
00111100+0011001100111100+00110011
j
10100010+00111010100010+001110
a 00011101+01100110 f 00111100+01111011
b 00100111+00111111 g 00111111+00111111
c 00101110+01001101 h 00110001+00111111
d 01110111+00111111 i. 01111111+011111
00111100+00110011 j 10100010+001110| a | $00011101+01100110$ | $f$ | $00111100+01111011$ |
| :---: | :---: | :---: | :---: |
| b | $00100111+00111111$ | g | $00111111+00111111$ |
| c | $00101110+01001101$ | h | $00110001+00111111$ |
| d | $01110111+00111111$ | i. | $01111111+011111$ |
| | $00111100+00110011$ | j | $10100010+001110$ |
Activity 1.9 活动 1.9
Convert the following denary numbers into binary and then carry out the binary addition of the two numbers and check your answer against the equivalent denary sum: 将下列二进制数转换成二进制数,然后将两个数进行二进制加法运算,并将答案与等价的二进制和进行核对:
a 98+1598+15
d 51+17151+171
g 19+13919+139
j 211+35211+35
b 29+8829+88
e 82+6982+69
h 203+30203+30
c 49+10049+100
f 100+140100+140
i 66+16666+166
Overflow 溢出
Now consider the following example: 现在请看下面的例子:
Example 3 示例 3
Add 01101110 and 11011110 (using bits) 添加 01101110 和 11011110(使用比特)
This addition has generated a 9th bit. The 8 bits of the answer are 01001100 - this gives the denary value (64+8+4)(64+8+4) of 76 which is incorrect because the denary value of the addition is 110+222=332110+222=332. 这个加法产生了第 9 位。答案的 8 位是 01001100 - 由此得到的面值 (64+8+4)(64+8+4) 是 76,这是不正确的,因为加法的面值是 110+222=332110+222=332 。
The maximum denary value of an 8 -bit binary number is 255 (which is 2^(8)-12^{8}-1 ). The generation of a 9 th bit is a clear indication that the sum has exceeded this value. This is known as an overflow error and in this case is an indication that a number is too big to be stored in the computer using 8 bits. 8 位二进制数的最大示值是 255(即 2^(8)-12^{8}-1 )。第 9 位的产生清楚地表明总和已经超过了这个值。这就是所谓的溢出错误,在这种情况下,表明一个数字太大,无法用 8 位存储在计算机中。
The greater the number of bits which can be used to represent a number then the larger the number that can be stored. For example, a 16-bit register would allow a maximum denary value of 65535 (i.e. 2^(16)2^{16} - 1) to be stored, a 32 -bit register would allow a maximum denary value of 4294967295 (i.e. 2^(32)-12^{32}-1 ), and so on. 可用于表示数字的位数越多,可存储的数字就越大。例如,16 位寄存器可存储的最大面值为 65535(即 2^(16)2^{16} -1),32 位寄存器可存储的最大面值为 4294967295(即 2^(32)-12^{32}-1 ),以此类推。
Activity 1.10 活动 1.10
1 Convert the following pairs of denary numbers to 8 -bit binary numbers and then add the binary numbers. Comment on your answers in each case: 1 将下列一对二进制数转换成 8 位二进制数,然后将二进制数相加。对每种情况的答案进行评述:
a 89+17589+175
b 168+99168+99
c 88+21588+215
2 Carry out the following 16-bit binary additions and comment on your answers: a 0111111111110001+01011111001110010111111111110001+0101111100111001 2 执行下列 16 位二进制加法运算,并对答案作出评述: a 0111111111110001+01011111001110010111111111110001+0101111100111001
b 1110111000001011+11111101110110011110111000001011+1111110111011001
1.1.5 Logical binary shifts 1.1.5 逻辑二进制移位
Computers can carry out a logical shift on a sequence of binary numbers. The logical shift means moving the binary number to the left or to the right. Each shift left is equivalent to multiplying the binary number by 2 and each shift right is equivalent to dividing the binary number by 2. 计算机可以对二进制数序列进行逻辑移位。逻辑移位意味着将二进制数向左或向右移动。每次向左移动相当于二进制数乘以 2,每次向右移动相当于二进制数除以 2。
As bits are shifted, any empty positions are replaced with a zero - see examples below. There is clearly a limit to the number of shifts which can be carried out if the binary number is stored in an 8-bit register. Eventually after a number of shifts the register would only contain zeros. For example, if we shift 01110000 (denary value 112) five places left (the equivalent to multiplying by 2^(5)2^{5}, i.e. 32 ), in an 8 -bit register we would end up with 00000000 . This makes it seem as though 112 xx32=0112 \times 32=0 ! This would result in the generation of an error message. 在位移过程中,任何空位都会被 0 所取代--请参见下面的示例。如果将二进制数存储在 8 位寄存器中,移位的次数显然是有限的。最终,经过多次移位后,寄存器中将只包含 0。例如,如果我们将 01110000(面值 112)向左移动五位(相当于乘以 2^(5)2^{5} ,即 32),那么在 8 位寄存器中,最终结果将是 00000000。这就好像是 112 xx32=0112 \times 32=0 !这样就会产生一条错误信息。
? Example 1 ?示例 1
The denary number 21 is 00010101 in binary. If we put this into an 8 -bit register: 21 的二进制数为 00010101。如果我们将其放入一个 8 位寄存器:
The left-most bit is often referred to as the MOST SIGNIFICANT BIT 最左边的位通常被称为最重要位(MOST SIGNIFICANT BIT)。
The left-most bit is now lost following a left shift 左移后,最左边的位将丢失
Suppose we now shift the original number two places left: 假设我们现在将原来的数字向左移动两位:
The binary number 1010100 is 84 in denary - this is 21 xx2^(2)21 \times 2^{2}. 二进制数 1010100 的面值为 84 - 即 21 xx2^(2)21 \times 2^{2} .
And now suppose we shift the original number three places left: 现在,假设我们将原来的数字向左移动三位:
The binary number 10101000 is 168 in denary - this is 21 xx2^(3)21 \times 2^{3}. 二进制数 10101000 的面值为 168,即 21 xx2^(3)21 \times 2^{3} 。
So, let us consider what happens if we shift the original binary number 00010101 four places left: 因此,让我们考虑一下,如果将原来的二进制数 00010101 向左移动四位会发生什么情况:
Losing 1 bit 丢失 1 位
following a shift 轮班后
Losing 1 bit
following a shift| Losing 1 bit |
| :--- |
| following a shift |