【深入理解计算机系统】 一:数值表示与编码

本课程内容来自于悉尼大学ELEC1601:introduction to the computer system

2.1. Binary Logic

The digital electronic circuits are capable of manipulating signals in two possible states: zero and one. As a consequence, all the information processed by these circuits needs to be first encoded using sets of the signals called bits. A digital circuit is then a system capable of receiving data encoded with a set of bits (inputs), perform some operation, and producing another set of signals (outputs) with the result. A microprocessor is a complex digital circuit capable of executing a previously defined set of instructions encoded with bits and known as Machine Language.

But in order for a microprocessor to execute these instructions, all their elements (numbers, symbols, operands, etc.) need to be encoded with binary logic. This encoding is the base to understand how a computer which is made mostly of digital circuits capable of manipulating bits can perform much more complex tasks such as allowing you to browse through the net, making a phone call, playing a game, listening to music, read your heart beat, etc.

In the following sections we will manipulate numbers in different bases, mostly in base 10, base 8 (or octal) and base 16 (or hexadecimal). You will need quite frequently to translate numbers between those bases. Even though the steps to make these translations will be explained, it is very helpful to carry out these operations with a calculator, or more precisely, a programmers calculator that offer the operations typical of this context.

These programmers calculators usually come installed by default in the most popular operating systems. The following figure shows you the ones currently present in Microsoft Windows, MacOS and some distributions of Linux.

Pre-installed Programmer Calculator

2.1.1. Properties of a binary encoding

Using a single bit, there are only two elements that can be represented, one for each of the possible values 0 and 1. If we use sequence of two bits, the number of possible combinations increases to four values: 00, 01, 10, and 11. For every bit we add to the sequence, the number of possible combinations doubles because we can repeat the combinations twice by adding a one to the first group and a zero to the second group.

If a single bit can encode only two elements, and for each additional bit we include in a sequence we double the number of combinations, in general with 

LaTeX: n
 bits we can encode up to 
LaTeX: 2^n
 elements.

The previous formula returns the number of possible combinations when using a sequence of 

LaTeX: n
n bits, but if we have a set with 
LaTeX: N
 elements, how many bits are needed to encode its elements in binary logic? For example, suppose the elements are 
LaTeX: \lbrace red,\:green,\:blue,\:cyan,\:magenta\rbrace
, is it possible to encode then in binary logic with two bits? with three? with four? The answer to this question is that when attempting to encode a set with 
LaTeX: N
 elements, the number of possible combinations must be larger or equal than the number of elements. In other words, then number of bits 
LaTeX: n
 required to encode the elements of a set with 
LaTeX: N
 elements must satisfy

(1) 

LaTeX: N\le2^n

Or alternatively, given a set with 

LaTeX: N
 elements, the minimum number of bits required to encode its elements with binary logic is

(2)

LaTeX: n\ge\lceil\log_2N\rceil

where the symbols ⌈ ⌉ represent the integer larger than the obtained logarithm.

Going back to the previous example, the number of bits required to encode the set of five elements is 

LaTeX: n\ge\log_25
, and therefore 
LaTeX: n\ge2.3219281
, so we need at least 3 bits.

This inequality is useful to know the minimum number of bits to encode a set, but it does not provide any statement about the maximum. When encoding a set of elements, using more bits than the minimum provided by equation (2) is perfectly feasible. An encoding may have a number of combinations larger than the number of elements in the set, thus leaving some of them unused.

The two previous equations can be transformed into the two rules to take into account when encoding a set of elements using binary logic:

  1. With 
    LaTeX: n
     bits we can encode up to a maximum of 
    LaTeX: 2^n
     elements.
  2. To encode 
    LaTeX: N
     elements in a set we need at least 
    LaTeX: \lceil\log_2\:N\rceil

Once the number of bits has been decided, you need to define a relationship between each element in the set and a concrete sequence of bits. Each element must have at least one binary representation, and each sequence of bits must correspond with one element in the set. This relationship must satisfy some minimum requirements to be usable by digital circuits. These circuits can only manipulate sequences of bits up to a certain length which needs to be decided beforehand. For example, if a circuit must operate with natural numbers, you need to decide which subset of those numbers will be used and from there, the number of bits required for their encoding.

2.2. Representing numbers in different bases

Before studying how to encode elements in different sets so that they can be manipulated by a computer system, it is very useful to study how to represent natural numbers in different bases. Conventionally, we write natural numbers using 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) in what is known as base 10. Given a number written in base 10, the least significant digit which is written in the right most position corresponds with the units. We will use the numbers in base 10 which are the ones normally used, to explain how to generalize this representation to any base. The digit in the left most position is the most significant. Any number represented in a base 

LaTeX: b
 satisfies the following conditions:
  • LaTeX: b
     digits are used to represent numbers. The digits go from 0 to 
    LaTeX: b-1
    .
  • The number represented by 
    LaTeX: b-1
     is followed by the number 10.
  • Analogously, the maximum number with d digits is followed by one with 
    LaTeX: d+1
     digits in which the most significant is 1 and the rest are all zeros.

These conditions are satisfied by numbers encoded in base 10 using digits 0 to 9. The number following 9 is 10, and the maximum number represented by, for example, 4 digits (which is 9999) is followed by 1 and five zeros (10000). The value of a sequence of digits is obtained multiplying each of them by the base raised to the exponent denoting its position starting by the least significant one being zero as shown in the following example.


alert_circle_icon_gray.png Example

LaTeX: 3214=3\ast1000+2\ast100+1\ast10+4

The same number can be re-written using the base raised to the appropriate exponent as follows:

LaTeX: 3214=3\ast10^3+2\ast10^2+1\ast10^1+4\ast10^0

The general formula to obtain the value of any number on any base can be re-written as 

LaTeX: 3214=\sum^3_{i=0}\left(d_i\ast base^i\right)=\left(d_0\ast10^0\right)+\left(d_1\ast10^1\right)+\left(d_2\ast10^2\right)+\left(d_3\ast10^3\right)

where 

LaTeX: d_i
 represents the digit in position 
LaTeX: i
 in the number. the formula applies to any base. Thus, the value of any n-digit number in base 
LaTeX: b
 is obtained by the following equation:

(3) 

LaTeX: \sum^{n-1}_{i=0} (d_{i} \ast b^{i} )

alert_circle_icon_gray.png Example

The equivalent in base 10 of the number 6342 in base 7 can be obtained by using equation (3):

LaTeX: 6342_7=\sum_{i=0}^3\left(d_i\ast7^i\right)

LaTeX: =\left(2\ast7^0\right)+\left(4\ast7^1\right)+\left(3\ast7^2\right)+\left(6\ast7^3\right)

LaTeX: =2+28+147+2058

LaTeX: =2235_{10}

Thus, number 6342 in base 7 corresponds to 2235 represented in base 10. As you can see, the number in base seven does not have any digit bigger than 6, because only the digits from 0 to 6 are allowed.


When writing numbers in different bases together we have an ambiguity problem. For example, the number 2235 in base 10 from the previous example is the equivalent of number 6342 in base 7. When we need to distinguish numbers encoded in bases different than base 10, the base is usually included in the right side of the number as a subscript. Thus we can write 

LaTeX: 2235_{10}=6342_7

Review questions for 2.2. Representing numbers in different bases

2.2.1. Translating numbers to different base encodings

Equation (3) allows to obtain value in base 10 of any number in any base. The opposite process, that is, given a number in base 10 obtain its equivalent in a different base requires repeated divisions by the base to obtain the digits of the new number starting by the least significant. The process is easily shown with an example. Suppose you want to calculate the equivalent in base 7 of the number 886751088675108867510. The least significant digit can be obtained by using equation (3).

(4) 

LaTeX: \sum^{n-1}_{i=0}\left(d_i\ast base^i\right)=d_0+\left(base\ast\sum^{n-1}_{i=i}\left(d_i\ast base^{i-1}\right)\right)

If equation (4) is divided by the base we obtain as remainder the least significant bit 

LaTeX: d_0
 of the representation. The quotient contains the rest of digits and if we keep divided by the base, the rest of the digits in the new base are obtained.
alert_circle_icon_gray.png Example

Consider the number 

LaTeX: 8675_{10}
 To obtain its representation in base 7 we perform the first division by 7 to obtain a remainder of 2, thus being the least significant digit. The quotient obtained is 1239. When divided again by 7, the remainder is 0, and the new quotient is 177. If this operation is repeated the successive remainders are the digits of the number in base 7 as shown in the following equation:

LaTeX: 8675=\left(1239\ast7\right)+2

LaTeX: =\left(\left(\left(177\ast7\right)+0\right)\ast7\right)+2

LaTeX: =\left(\left(\left(\left(\left(25\ast7\right)+2\right)\ast7\right)+0\right)\ast7\right)+2

LaTeX: =\left(\left(\left(\left(\left(\left(\left(3\ast7\right)+4\right)\ast7\right)+2\right)\ast7\right)+0\right)\ast7\right)+2

The successive divisions by the base guarantees that eventually a quotient with value less than the base. When this occurs there is no need for more divisions. The representation in base 7 can be used to apply equation (3) and obtain again the number in base 10:

LaTeX: 8675=\left(3\ast7^4\right)+\left(4\ast7^3\right)+\left(2\ast7^2\right)+\left(0\ast7^1\right)+\left(2\ast7^0\right)

and therefore

LaTeX: 8675_{10}=34202_7

In summary, given a number in base 10, its representation in another base 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值