YP.2.4 Binary-Decimal Conversion(双语)

from:

[1] Introduction to Computing System, from Bits and Gates to C and Beyond. Yale N. Patt and Sanjay J. Patel. McGraw-Hill Higher Education.(英)

[2] 计算系统基础_陈道蓄http://course.jingpinke.com/details?uuid=8a833999-2031c13b-0120-31c13bb1-012b

2.4 Binary-Decimal Conversion

It is often useful to convert integers between the 2's complement data type and the decimal representation that you have used all your life.

2.4.1 Binary to Decimal Conversion

We convert from 2's complement to a decimal representation as follows: For purposes of illustration, we will assume 2's complement representations of eight bits, corresponding to decimal integer values from -128 to +127.

Recall that an eight-bit 2's complement number takes the form 

a7 a6 a5 a4 a3 a2 a1 a0

where each of the bits ai is either 0 or 1.

1. Examine the leading bit a7. If it is a 0, the integer is positive, and we can begin evaluating its magnitude. If it is a 1, the integer is negative. In that case, we need to first obtain the 2's complement representation of the positive number having the same magnitude.

2. The magnitude is simply

a6 • 26 + a5 • 25 +a4 •24 +a3 • 23 +a2 • 22 +a1 • 21 +a0 • 20

which we obtain by simply adding the powers of 2 that have coefficients of 1.

3. Finally, if the original number is negative, we affix a minus sign in front.

Done!

Example 2.2

Convert the 2's complement integer 11000111 to a decimal integer value.

1. Since the leading binary digit is a 1, the number is negative. We must first find the 2's complement representation of the positive number of the same magnitude. 

This is 00111001.

2. The magnitude can be represented as

0 • 26 + • 25 + 1 • 24 + 1 • 23 + • 22 +0 • 21 + 1 • 20

or

32 + 16 + 8 + 1.

3. The decimal integer value corresponding to 11000111 is -57.

 

2.4.2 Decimal to Binary Conversion

Converting from decimal to 2's complement is a little more complicated. The crux of the method is to note that a positive binary number isodd if the rightmost digit is 1 andeven if the rightmost digit is 0.

Consider again our generic eight-bit representation:

a7 • 27 + a6 • 26 +a5 • 25 +a4 •24 +a3 • 23 +a2 • 22 +a1 • 21 +a0 • 20

We can illustrate the conversion best by first working through an example.

Suppose we wish to convert the value +105 to a 2's complement binary code. We note that +105 is positive. We first find values forai, representing the magnitude 105. Since the value is positive, we will then obtain the 2's complement result by simply appendinga7, which we know is 0.

Our first step is to find values for ai that satisfy the following:

105 = a6 • 26 + a5 • 25 +a4 •24 +a3 • 23 +a2 • 22 +a1 • 21 +a0 • 20

Since 105 is odd, we know that a0 is 1. We subtract 1 from both sides of the equation, yielding

104 = a6 • 26 + a5 • 25 +a4 •24 +a3 • 23 +a2 • 22 +a1 • 21

We next divide both sides of the equation by 2, yielding

52 = a6 • 25 + a5 • 24 +a4 •23 +a3 • 22 +a2 • 21 +a1• 20 

We note that 52 is even, so a1 the only coefficient not multiplied by a power of 2, must be equal to 0.

We now iterate the process, each time subtracting the rightmost digit from both sides of the equation, then dividing both sides by 2, and finally noting whether the new decimal number on the left side is odd or even. Starting where we left off, with

52 = a6 • 25 + a5 • 24 +a4 •23 +a3 • 22 +a2 • 21

the process produces, in turn:

26 = a6 • 24 + a5 • 23 +a4 •22 +a3 • 21 +a2 • 20

Therefore, a2 = 0.

13 = a6 • 23 + a5 • 22 +a4 •21 +a3 • 20

Therefore, a3 = 1.

6 = a6 • 22 + a5 • 21 +a4 •20

Therefore, a4 = 0.

3 = a6 • 21 + a5 • 20

Therefore,  a5 = 1.

1 = a6 • 20

Therefore, a6 = 1, and we are done. The binary representation is 01101001.

Let's summarize the process. If we are given a decimal integer valueN,we construct the 2's complement representation as follows:

1. We first obtain the binary representation of the magnitude ofNby forming the equation

N = a6 • 26 + a5 • 25 +a4 •24 +a3 • 23 +a2 • 22 +a1 • 21 +a0 • 20

and repeating the following, until the left side of the equation is 0:

a. If N is odd, the rightmost bit is 1. If N is even, the rightmost bit is 0.

b. Subtract 1 or 0 (according to whether N is odd or even) fromN,

remove the least significant term from the right side, and divide both sides of the equation by 2.

Each iteration produces the value of one coefficient ai.

2. If the original decimal number is positive, append a leading 0 sign bit, and you are done.

3. If the original decimal number is negative, append a leading 0 and then form the negative of this 2's complement representation, and then you are done.


2.4 二进制-十进制转换

我们经常需要在二进制补码数据类型和日常生活中使用的十进制表示之间进行转换。

2.4.1 二进制到十进制的转换

我们按如下方法从二进制补码向十进制转换:为了举例说明,我们假设使用8位的二进制补码表示,相应的十进制数值从-128到127。

一个8位的二进制补码数采取如下格式:

a7 a6 a5 a4 a3 a2 a1 a0

其中的每个ai要么是0要么是1。

1. 检查最前面的a7。如果是0,该整数是正数,我们就可以直接计算其数值。如果是1,该整数是负数,我们必须首先得到与其绝对值相同的正数的二进制补码表示(即“取反加1”)。

2. 通过简单的计算:

a6*26+a5*25+a4*24+a3*23+a2*22+a1*21+a0*20

得到该数值。我们只需将那些系数为1的2的幂次简单相加,就可以得到该数值。

3. 最后,如果原数值是负数,我们在前面加一个负号前缀即可。

例 2.2 将二进制补码整数11000111转换为十进制数值。

1. 既然最前面的一位是1,则该数值是负数。我们首先必须得到与其绝对值相同的正数的二进制表示,为00111001。

2.该数值可以通过计算:

0*26+1*25+1*24+1*23+0*22+0*21+1*20

或32+16+8+1得到,为57。

3.11000111对应的十进制数值是-57。

2.4.2 十进制到二进制的转换—方法1

把一个数从十进制转换成二进制复杂一些。这种方法最重要的依据是:如果一个正的二进制数的最右端的数字为1,则这个数为奇数;否则为偶数。

再来看一个通用的八位二进制数的表示:

a7*27+a6*26+a5*25+a4*24+a3*23+a2*22+a1*21+a0*20

还是通过一个例子来具体说明这种转换:

假设我们想把十进制数+105转化成二进制补码表示。我们注意到+105是正数。首先我们要找到表示105的ai的值。由于该数值为正数,则二进制补码表示的最高位a7为0。

    第一步是找到满足下列等式的ai的值: 

105=a6*26+a5*25+a4*24+a3*23+a2*22+a1*21+a0*20

    由于105是奇数,可知a0=1,在等式两端同时减去1可得:

104= a6*26+a5*25+a4*24+a3*23+a2*22+a1*21

    下一步,在等式两端同时除以2,得:

52=a6*25+a5*24+a4*23+a3*22+a2*21+a1*20

    52为偶数,所以a1一定为0。

    现在,我们重复这个过程:在等式两端同时减去最右端的数,在等式两端同时除以2,然后我们观察新得到的等号左端的十进制数是奇还是偶。再来看我们刚才的例子

52=a6*25+a5*24+a4*23+a3*22+a2*21

经过以上的步骤,结果为:

26= a6*24+a5*23+a4*22+a3*21+a2*20

所以,a2=0。

13= a6*23+a5*22+a4*21+a3*20

所以,a3=1。

6= a6*22+a5*21+a4*20

所以,a4=0。

3= a6*21+a5*20

所以,a5=1。

1= a6*20

所以,a6=1,我们得到了最后的结果:二进制表示为01101001。

    我们总结解题过程如下:已知十进制数N,通过以下步骤可得其补码表示:

1. 首先将N的绝对值的二进制码字用等式表示出来:

N= a6*26+a5*25+a4*24+a3*23+a2*22+a1*21+a0*20

重复以下步骤直至左边等式数字为0:

a. 如果N为奇,则最右边一位数为1;如果N为偶,则最右边一位数为0;

b. 两边同时减去1或0(若N为奇,则减去1;若N为偶,则减去0),然后两边同时除以2;

每一次重复这一步可得到一个ai的值。

2. 如果原来的十进制数为正,则在二进制数前加0,得到结果;

3. 如果原来的十进制数为负,在二进制数前加0,然后再计算出这个补码的负数,得到结果。

2.4.3 十进制到二进制的转换—方法2

“除2取余”(略)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值