1. sum = 1+1+0 = 10 (2), c[1] = 2 % 2 = 0, c[2] = 2/2 = 1
2. sum = 1+1+1 = 11 (3), c[2] = 3 % 2 = 1, c[3] = 3/2 = 1
3. sum = 0+0+1 = 01 (1), c[3] = 1 % 2 = 1, c[4] = 1/2 = 0
4. sum = 1+0+0 = 01 (1), c[4] = 1 % 2 = 1, c[5] = 1/2 = 0
^ ^ ^ ^ ^
i A B C, all at position i note that we store the carry for the NEXT step
结果:C = 01110 (14)
C is both the solution and the carry. For a real example, let's add 11 + 3. I'll write in binary with decimal in parens)
A = 1011 (11) + B = 0011 (3) [C starts as 00000 (0)]
^ ^ ^
The ^s mark the first position, and we go left, since we read left to right, but math goes right to left. Also, we divide integers, so 3/2 = 1, not 1.5. Now the steps.
1. sum = 1+1+0 = 10 (2), c[1] = 2 % 2 = 0, c[2] = 2/2 = 1
2. sum = 1+1+1 = 11 (3), c[2] = 3 % 2 = 1, c[3] = 3/2 = 1
3. sum = 0+0+1 = 01 (1), c[3] = 1 % 2 = 1, c[4] = 1/2 = 0
4. sum = 1+0+0 = 01 (1), c[4] = 1 % 2 = 1, c[5] = 1/2 = 0
^ ^ ^ ^ ^
i A B C, all at position i note that we store the carry for the NEXT step
C[i+1] 被分配为 sum / 2 因为 sum / 2 是进位金额。当我们对 i = i + 1 执行 A[i] + B[i] + C[i] 时,它将在下一次迭代中使用。
You add C[i] as well because C[i] may contain a carry bit from when you added A[i-1] + B[i-1] + C[i-1] in the previous iteration. For example if we do 1 + 1, our first iteration sum = 1 + 1 + 0 = 2, but since this is binary we have to carry the 1 and put it on C[1] and put the remainder (2 % 2 = 0) in C[0]. This gives us 10
C[i] gets sum % 2 because the sum of A[i] + B[i] + C[i] could be more than 1, but 1 is the most that will fit in that digit. The rest of the sum (if there is any) will be put in the carry bit. And that brings us to...
C[i+1] gets assigned sum / 2 because sum / 2 is the carry amount. It will be used in the next iteration when we do A[i] + B[i] + C[i] for i = i + 1.
You can think of adding binary numbers the same way you add base 10 numbers: there is an "add" step and a "carry" step to perform at each digit.
So, let's take the math one bit at a time. Say we're adding:
101
+
011
For the first step, we start on the far-right. (In your example, this corresponds to i=1). We add (1+1)%2, which gives us 0. What's really going on here? 1+1 is 2, which in binary is a two-digit number ("10"). We can only write the lower-order digit ("0"), so expressing the sum "mod 2" is really just saying "don't worry about the carry-over sum for now." So we've got:
101
+
011
---
0 (carrying a 1)
Now we implement the "carry a 1" by doing integer division ("sum / 2"), and temporarily storing it:
101
+
011
---
10
Now we are ready to add the 2nd digits: (0+1)%2 -- but we must add in the carry-over 1 that we've been keeping track of, so we take (0+1+1)%2 yielding:
101
+
011
---
00
Again we need to keep track of carry bit, giving us (0+1+1)=1:
101
+
011
---
100
Finally we add the 3rd bits: (1+0+1)%2 to give the answer:
发布评论
评论(3)
C既是解又是进位。举一个真实的例子,让我们加 11 + 3。我会用二进制写,括号里有十进制)
^s 标记第一个位置,我们向左走,因为我们从左到右读取,但数学从右到左读取。另外,我们除以整数,因此 3/2 = 1,而不是 1.5。现在步骤。
结果:C = 01110 (14)
C is both the solution and the carry. For a real example, let's add 11 + 3. I'll write in binary with decimal in parens)
The ^s mark the first position, and we go left, since we read left to right, but math goes right to left. Also, we divide integers, so 3/2 = 1, not 1.5. Now the steps.
Result: C = 01110 (14)
C[i]
,因为C[i]
可能包含添加A[i-1] + B[i- 时的进位位1] + C[i-1]
在上一次迭代中。例如,如果我们执行1 + 1
,我们的第一次迭代sum = 1 + 1 + 0 = 2
,但由于这是二进制,我们必须携带 1 并将其放入放在C[1]
上,并将余数 (2 % 2 = 0
) 放入C[0]
中。这让我们10
C[i]
得到 sum % 2,因为A[i] + B[i] + C[i]
的总和> 可以大于 1,但 1 是最适合该数字的值。其余的和(如果有的话)将被放入进位位。这让我们...C[i+1]
被分配为sum / 2
因为sum / 2
是进位金额。当我们对i = i + 1
执行A[i] + B[i] + C[i]
时,它将在下一次迭代中使用。C[i]
as well becauseC[i]
may contain a carry bit from when you addedA[i-1] + B[i-1] + C[i-1]
in the previous iteration. For example if we do1 + 1
, our first iterationsum = 1 + 1 + 0 = 2
, but since this is binary we have to carry the 1 and put it onC[1]
and put the remainder (2 % 2 = 0
) inC[0]
. This gives us10
C[i]
gets sum % 2 because the sum ofA[i] + B[i] + C[i]
could be more than 1, but 1 is the most that will fit in that digit. The rest of the sum (if there is any) will be put in the carry bit. And that brings us to...C[i+1]
gets assignedsum / 2
becausesum / 2
is the carry amount. It will be used in the next iteration when we doA[i] + B[i] + C[i]
fori = i + 1
.您可以将二进制数相加视为以 10 为基数相加的相同方式:对每个数字执行一个“相加”步骤和一个“进位”步骤。
那么,让我们一次一点地进行数学计算。假设我们要添加:
第一步,我们从最右边开始。 (在您的示例中,这对应于 i=1)。我们加上 (1+1)%2,得到 0。这里到底发生了什么? 1+1 是 2,在二进制中是一个两位数(“10”)。我们只能写低位数字(“0”),因此表达总和“mod 2”实际上只是在说“现在不用担心结转总和”。所以我们得到了:
现在我们通过整数除法(“sum / 2”)来实现“进位 1”,并暂时存储它:
现在我们准备添加第二个数字:( 0+1)%2——但是我们必须添加我们一直在跟踪的进位 1,因此我们采用 (0+1+1)%2 产生:
我们再次需要跟踪进位位,给我们 (0+1+1)=1:
最后我们添加第 3 位:(1+0+1)%2 给出答案:
You can think of adding binary numbers the same way you add base 10 numbers: there is an "add" step and a "carry" step to perform at each digit.
So, let's take the math one bit at a time. Say we're adding:
For the first step, we start on the far-right. (In your example, this corresponds to i=1). We add (1+1)%2, which gives us 0. What's really going on here? 1+1 is 2, which in binary is a two-digit number ("10"). We can only write the lower-order digit ("0"), so expressing the sum "mod 2" is really just saying "don't worry about the carry-over sum for now." So we've got:
Now we implement the "carry a 1" by doing integer division ("sum / 2"), and temporarily storing it:
Now we are ready to add the 2nd digits: (0+1)%2 -- but we must add in the carry-over 1 that we've been keeping track of, so we take (0+1+1)%2 yielding:
Again we need to keep track of carry bit, giving us (0+1+1)=1:
Finally we add the 3rd bits: (1+0+1)%2 to give the answer: