大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
二进制的加减法
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,
在添加时,
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:
使用以上提供的规则,求和运算可以按以下方式执行:
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 ,因此当我们将2和3相加时,总和为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:
使用以上提供的规则,求和运算可以按以下方式执行:
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:
使用以上提供的规则,求和运算可以按以下方式执行:
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:
解:
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:
解:
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
二进制的加减法
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/189739.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...