Monday, July 12, 2010

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)
spr
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.
spr
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 !!) 

reference: helpwithpcs.com

Adding Binary Numbers


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 (carry one)
1 + 1 (+ the carry) = 1 (carry one)
0 + 1 (+ the carry) = 0 (carry one)
1 + 0 (+ the carry) = 0 (carry one)
1 + 0 (+ the carry) = 0 (carry one)
0 + 1 (+ the carry) = 0 (carry one)
1 + 0 (+ the carry) = 0 (carry one)

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

reference: helpwithpcs.com

Number Systems and Conversions

Numbers can be represented in a variety of ways. The representation depends on what is called the BASE. The following are the four most common representations.

Decimal:
We normally represent numbers in their decimal form. Numbers in decimal form are in
base 10. This means that the only digits that appear are 0-9. Here are examples of
numbers written in decimal form:
12610 (normally written as just 126)
1110 (normally written as just 11)

Binary:
Numbers in binary form are in base 2. This means that the only legal digits are 0 and 1. We need to write the subscript 2 to indicate that the number is a binary number. Hereare examples of numbers written in binary form:
11111102
10112

Decimal to Binary / Binary to Decimal:
To convert a decimal number to binary, continuously divide the number by 2 and get the remainder (which is either 0 or 1), and get that number as a digit of the binary form of the number. Get the quotient and divide that number again by 2 and repeat the whole process until the quotient reaches 0 or 1. We then get all the remainders starting from the last remainder, and the result is the binary form of the number.

NOTE: For the last digit which is already less than the divisor (which is 2) just copy the value to the remainder portion. For Example:










Binary number to Decimal:
we multiply the binary digit to "2 raised to the position of the binary number". We then add all the products to get the resulting decimal number. For Example: