前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >二进制中的加减法_二进制数的加减运算

二进制中的加减法_二进制数的加减运算

作者头像
全栈程序员站长
发布2022-11-08 18:35:54
9610
发布2022-11-08 18:35:54
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

二进制的加减法

1)二进制加法 (1) Binary Addition)

Since binary numbers consist of only two digits 0 and 1, so their addition is different from decimal addition. Addition of binary numbers can be done following certain rules:

由于二进制数仅由两位数字0和1组成,因此它们的加法与十进制加法不同。 可以按照某些规则添加二进制数

A

B

Sum

Carry

0

0

0

0

0

1

1

0

1

0

1

0

1

1

0

1

一个

携带

0

0

0

0

0

1个

1个

0

1个

0

1个

0

1个

1个

0

1个

The above table contains two bits a and b, their sum and carry.

上表包含两个位a和b,它们的和与进位。

On adding,

在添加时,

代码语言:javascript
复制
    0 + 0 = 0, 	
    0 + 1 = 1,	
    1 + 0 = 1,	
    1 + 1 = 10 (i.e., sum is 0 and carry is 1)

Let’s do some exercise and solution some questions based on binary addition to get more of the topic.

让我们做一些练习,并根据二进制加法解决一些问题,以获取更多的主题。

Example 1: Perform (10)2 + (11)2

范例1:执行(10) 2 +(11) 2

Solution:

解:

Using the rules provided above, sum operation can be performed as:

使用以上提供的规则,求和运算可以按以下方式执行:

Binary addition example 1
Binary addition example 1

Therefore, (10)2 + (11)2 = (101)2

因此, (10) 2 +(11) 2 =(101) 2

Verification:

验证:

We can verify our result by converting the above binary numbers into decimal numbers and then verifying the sum.

我们可以通过将上述二进制数字转换为十进制数字然后验证总和来验证结果。

Here, (10)2 = (2)10, (11)2 = (3)2 and (101)2 = (5)10, thus when we will add 2 and 3 we get sum as 5.

在这里, (10) 2 =(2) 10(11) 2 =(3) 2(101) 2 =(5) 10 ,因此当我们将23相加时,总和为5



Example 2: Perform (1)2 + (1)2 + (1)2 + (1)2

示例2:执行(1) 2 +(1) 2 +(1) 2 +(1) 2

Solution:

解:

Using the rules provided above, sum operation can be performed as:

使用以上提供的规则,求和运算可以按以下方式执行:

Binary addition example 2
Binary addition example 2


Example 3: Perform (110)2 + (111)2 + (101)2

示例3:执行(110) 2 +(111) 2 +(101) 2

Solution:

解:

Using the rules provided above, sum operation can be performed as:

使用以上提供的规则,求和运算可以按以下方式执行:

Binary addition example 3
Binary addition example 3

Verification:

验证:

We can verify our result as (110)2=(6)10, (111)2=(7)10, (101)2= (5)10 and (10010)2= (18)10. So when we will add 6 + 7 + 5 =18, which we are getting as our answer.

我们可以验证结果为(110) 2 =(6) 10(111) 2 =(7) 10(101) 2 =(5) 10(10010) 2 =(18) 10 。 因此,当我们添加6 + 7 + 5 = 18时 ,我们将以此作为答案。

2)二进制减法 (2) Binary Subtraction)

The binary subtraction is performed like decimal subtraction, the rules for binary subtraction are:

二进制减法的执行方式类似于十进制减法,二进制减法的规则为:

A

B

Difference

Borrow

0

0

0

0

0

1

1

1

1

0

1

0

1

1

0

0

一个

区别

0

0

0

0

0

1个

1个

1个

1个

0

1个

0

1个

1个

0

0

Example 1: Subtract (10)2 from (1001)2

实施例1:减法(10)2从(1001)2

Solution:

解:

Binary subtraction example 1
Binary subtraction example 1

In column C2, 1 can’t be subtracted from 0 so, we have to borrow 1 from column C3, but C3 also has a 0, so 1 must be borrowed from column C4, the 1 borrowed from column C4 becomes 10 in column C3, now keeping 1 in column C3 bringing the remaining 1 to column C2 which becomes 10 in column C2 thus 10 – 1= 1 in column C2.

在C 2列中,不能从0减去1,因此,我们必须从C 3列中借用1,但是C 3也有0,因此必须从C 4列中借用1,从C 4列中借用1。在列C 3成为如图10所示,现在在列C 3保持1使剩余的1至列C 2,其在列C 2变为10因此10 – 1 = 1在列C 2中 。

In column C3, 1 – 0 = 1

在C 3列中,1 – 0 = 1

In column C4, 1 after providing borrow 1 is reduced to 0.

在C 4列中,提供借位1后的1减少为0。

Therefore, (1001)2 – (10)2 = (111)2

因此, (1001) 2 –(10) 2 =(111) 2



Example 2: Subtract (111.111)2 from (1010.01)2

实施例2:减法(111.111)从2(1010.01)2

Solution:

解:

Binary subtraction example 2
Binary subtraction example 2

In Column C0, 1 can’t be subtracted from 0, so we have to borrow 1 from column C1, which becomes 10 in column C0, thus 10 – 1 = 1,

在C 0列中,不能从0中减去1,因此我们必须从C 1列中借用1 ,在C 0列中它变为10,因此10 – 1 = 1,

In column C1, after providing borrow 1 to C0, C1 is reduced to 0. Now 1 can’t be subtracted from so borrow 1 from C2, but it is also 0, so borrow 1 from C3 which is also 0, so borrow 1 from C4, reducing column C4 to 0. Now, this 1 borrowed from column C4 becomes 10 in column C3, keep 1 in the column C3 and bring other 1 to column C2, which makes column C2 as 10 now again bring 1 from C2 to C1, which reduces C2 to 1 and makes C1 as 10.

在C 1列中,向C 0提供借位1之后,C 1减少为0。现在不能从中减去1,因此从C 2借出1,但是它也为0,因此从C 3借出1也是0,所以由C 4借1,减少列C 4至0。现在,这个1从列C 4借变成10在列C 3中,保持1中的列C 3和带来其它1至柱C 2,这使得列C 2为10现在又将1从C 2带到C 1 ,这将C 2减少为1并使C 1为10。

Thus, In Column C1, 10 – 1 = 1

因此,在列C 1中 ,10 – 1 = 1

In Column C2, 1 – 1 = 0

在C 2列中,1 – 1 = 0

In Column C3, 1 – 1 = 0

在C 3列中,1 – 1 = 0

In Column C4, we now have 1 to be subtracted from 0 which is not possible so we will borrow 1 from Column C5, but Column C5 has a 0 so borrow 1 from C6 making C6 to be 0 and bring it to C5 which makes it 10 in C5, keep 1 in C5 and bring the other 1 to C4 which makes C4 as 10 thus

在C 4列中,现在不可能从0中减去1,这是不可能的,因此我们将从C 5列中借入1,但是C 5列具有0,因此从C 6中借入1,从而使C 6为0并将其取为零。到C 5,这使得它在10的C 5,保持1中的C 5和使其他1至C 4,这使得-C 4作为10因此

In column C4, 10 – 1 = 1

在C 4列中,10 – 1 = 1

In column C5, 1 – 1 = 0

在C 5列中,1-1 = 0

In column C6, 0 – 0 = 0

在C 6列中,0 – 0 = 0

Hence, the result is (1010.01)2 – (111.111)2 = (0010.011)2

因此,结果为(1010.01) 2 –(111.111) 2 =(0010.011) 2

翻译自: https://www.includehelp.com/basics/binary-addition-and-subtraction.aspx

二进制的加减法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月25日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1)二进制加法 (1) Binary Addition)
  • 2)二进制减法 (2) Binary Subtraction)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档