As with decimal numbers, you can simply add up each column, and carry a 1 to the next column if the sum exceeds the base (which is ten for decimal, sixteen for hexadecimal, two for binary, etc.
However, it is much simpler for binary, since the resulting digit can only be ZERO or ONE.
Here are the rules for the two digits in the "ones-column":
- If both digits are different, then the result is one; if both digits are the same, then the result is zero. (This is exactly the same as performing an exclusive-or or "XOR" operation between the two bits.)
- Carrying-out to the next column is even simpler: if both digits are ONE then there is a carry-out; otherwise no carry-out.
For digits in the higher columns (2, 4, 8, etc.), there may or may not be a carry-in bit from the next-lower column. If there is no carry-in, the rules are the same as above (i.e., ZERO if both are the same; ONE if different; carry-out if both were ONE). However, if there is a carry-in (of ONE), then there are three bits (Binary-digITs) to consider, so both the result and the carry-out depend upon how many of these bits are ONE.
Here are the rules for when there is also a "carry-in" bit:
- If, among the three bits, an ODD number of them are ONE then the result is ONE; otherwise (if there is an EVEN number of ones), then the result is ZERO.
- There will be a carry-out if the number of one-bits is two or more (e.g., 111, 110, 101, 011) and no carry if not (e.g., 100, 010, 001, 000).
-- -- -- -- -- -- -- --
Below is an answer that illustrates the process, and also deals with negative numbers.
https://www.quora.com/What-is-the-sum-of-the-binary-numbers-101011-and-010101/answer/Bruce-Alan-Martin
What is the sum of the binary numbers 101011 and 010101?
Answered Nov 6, 2017
Lukas S. (and others) have already answered the question for the case where 101011 and 010101 both represent positive binary numbers:
101011 (in base 10 it is 43)
+ 010101 (in base 10 it is 21)
----------
1000000 (in base 10 it is 64)
Of course, this would constitute “overflow” on a binary computer with only six bits to represent the unsigned number. (Someone else gave the answer 11112, which would be correct for bases of three or greater! However, the less said about that answer, the better. ;^> )
The answer would be more complex if the leading one-bit represented a negative number (as Lukas suggested). For negative numbers, there are at least three different representations commonly used in binary computers:
- Sign-Magnitude, where the first bit represents the sign and the remaining bits contain the magnitude.
- Ones-Complement, where all bits are complemented to get the magnitude.
- Twos-Complement, where the magnitude is obtained by adding one to the ones complement.
Below are the answers for each of these three representation schemes:
0. ) Sign/Magnitude:
-
101011 (in S/M, this is -11)
-
+ 010101 (+21)
-
----------
-
001010 (in base ten, the answer is +10)
1. ) Ones-Complement:
-
101011 (010100 is the ones-complement, so this is -36)
-
+ 010101 (+21)
-
----------
-
110000 (in base ten, the answer is -15)
2. ) Twos-Complement:
-
101011 (010101 is the twos-complement, so this is -37)
-
+ 010101 (+21)
-
----------
-
111000 (in base ten, the answer is -16)
Other schemes include “offset”, where the all-zeroes represents the lowest possible value (such as -273 degrees Centigrade or 0 Kelvin), so it is not possible to interpret 101011 without knowing the offset.
3. ) Offset, with the range beginning at -273 (negative 0100010001):
-
101011 (43 above lowest value in range: -230)
-
+ 010101 (21 above lowest value in range: -252)
-
----------
-
1 000000 (in base ten, the answer is -482, which is
-
"out of range" and cannot be represented with 6 bits.)