Você está na página 1de 3

Adding binary numbers

Adding binary numbers is very similar to adding decimal numbers, first an example:

Let's look at the above example step by step: 1 1 0 1 1 0 1 + + + + + + + 1 1 1 0 0 1 0 = 0 (carry one) (+ the carry) = (+ the carry) = (+ the carry) = (+ the carry) = (+ the carry) = (+ the carry) = 1 0 0 0 0 0 (carry (carry (carry (carry (carry (carry one) one) one) one) one) one)

The last carry is placed at the left hand side of the result giving: 10000010

Subtracting binary numbers


The most common way of subtracting binary numbers is done by first taking the second value (the number to be subtracted) and apply what is known as two's complement, this is done in two steps: 1. complement each digit in turn (change 1 for 0 and 0 for 1). 2. add 1 (one) to the result. note: the first step by itself is known as one's complement. By applying these steps you are effectively turning the value into a negative number, and as when dealing with decimal numbers, if you add a negative number to a positive number then you are effectively subtracting to the same value. In other words 25 + (-8) = 17, which is the same as writing 25 - 8 = 17. An example, let's do the following subtraction 11101011 - 01100110 (23510 - 10210) note: When subtracting binary values it is important to maintain the same amount of digits for each number, even if it means placing zeroes to the left of the value to make up the digits, for instance, in our example we have added a zero to the left of the value 1100110 to make the amount of numerals up to 8 (one byte) 01100110. First we apply two's complement to 01100110

which gives us 10011010. Now we need to add 11101011 + 10011010, however when you do the addition you always disregard the last carry, so our example would be:

which gives us 10000101, now we can convert this value into decimal, which gives 13310 So the full calculation in decimal is 23510 - 10210 = 13310 (correct !!)

Negative numbers
The above example is subtracting a smaller number from a larger number, if you want to subtract a larger number from a smaller number (giving a negative result), then the process is slightly different. Usually, to indicate a negative number, the most significant bit (left hand bit) is set to 1 and the remaining 7 digits are used to express the value. In this format the MSB is referred to as the sign bit. Here are the steps for subtracting a large number from a smaller one (negative result). 1. Apply two's complement to the larger number. 2. Add this value to the smaller number. 3. Change the sign bit (MSB) to zero. 4. Apply two's complement to value to get final result. 5. The most significant bit (sign bit) now indicates the value is negative. For example let's do the following subtraction 10010101 - 10110100 (14910 - 18010) The process is as follows:

Now we can convert this value into a negative decimal, which gives -3110 So, the full calculation in decimal is 14910 - 18010 = -3110 (correct !!)

Multiplying binary numbers


Binary multiplication can be achieved in a similar fashion to multiplying decimal values. Using the long multiplication method, ie, by multiplying each digit in turn, and then adding the values together. For example, lets do the following multiplication: 1011 x 111 (decimal 1110 x 710)

which gives us 1001101, now we can convert this value into decimal, which gives 7710 So the full calculation in decimal is 1110 x 710 = 7710 (correct !!) note: Notice the pattern in the partial products, as you can see multiplying a binary value by two can be achieved by shifting the bits to the left and adding zeroes to the right.

Dividing binary numbers


Like multiplication, dividing binary values is the same as long division in decimal. For example, lets do the following division: 1001 11 (decimal 910 310)

which gives us 0011, now we can convert this value into decimal, which giveps 310 So the full calculation in decimal is 910 310 = 310 (correct!!) note: Dividing a binary value by two can also be achieved by shifting the bits to the right and adding zeroes to the left.

Você também pode gostar