Binary Addition
For example,
Note: The rules of binary addition (without carries) are the same as the truths of the XOR gate. Binary Subtraction
When a 0 borrows 1 from say ith left bit, then 1 at ith position becomes 0 and all 0's positioned from the ith to borrowing 0 become 1. Ones Complement The one's complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0's for 1's and viceversa). The one's complement of the number then behaves like the negative of the original number in most arithmetic operations. However, unlike two's complement, these numbers could not have widespread use because of issues like negative zero, endaround borrow, etc.
Addition of signed numbers in one's complement is performed using binary addition with endaround carry. If there is a carry out of the most significant bit of the sum, this bit must be added to the least significant bit of the sum.

0 0 0 1 0 0 0 1  =  17_{(base 10)} 
1 1 1 1 0 1 1 1  =  8_{(base 10)} 
0 0 0 0 1 0 0 0 
+ 1  
0 0 0 0 1 0 0 1  =  9_{(base 10)}
_{
}

Two's complement
Ones complement + 1
The two's complement of the number then behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way.
To calculate the 2's complement of an integer, invert the binary equivalent of the number by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement), and then add one.
For example,
0001 0001_{(binary 17)} = 1110 1111_{(two's complement 17)}  
NOT(0001 0001)  =  1110 1110 (Invert bits) 
1110 1110 + 0000 0001  =  1110 1111 (Add 1) 
2's Complement Addition
Two's complement addition follows the same rules as binary addition.For example,
5 + (3) = 2  0000 0101  =  +5  
+ 1111 1101  =  3  
0000 0010  =  +2 
2's Complement Subtraction
Two's complement subtraction is the binary addition of the minuend to the 2's complement of the subtrahend (adding a negative number is the same as subtracting a positive one).For example,
7  12 = (5)  0000 0111  =  +7  
+ 1111 0100  =  12  
1111 1011  =  5 
Note  in 2's compliment subtraction  if there is a carry over from the MSB, ignore that.
Example :: 1010  0001 = 1001
 Positive Numbers
 Positive 2's complement numbers are represented as the simple binary.
 Negative Numbers
 Negative 2's complement numbers are represented as the binary number that when added to a positive number of the same magnitude equals zero.
Integer  2's Complement  

Signed  Unsigned  
5  5  0000 0101 
4  4  0000 0100 
3  3  0000 0011 
2  2  0000 0010 
1  1  0000 0001 
0  0  0000 0000 
1  255  1111 1111 
2  254  1111 1110 
3  253  1111 1101 
4  252  1111 1100 
5  251  1111 1011 
 Note: The most significant (leftmost) bit indicates the sign of the integer; therefore it is sometimes called the sign bit.

 If the sign bit is zero,
 then the number is greater than or equal to zero, or positive.
 If the sign bit is one,
 then the number is less than zero, or negative.
Negative zero
0 = 0000 0000
~0 = 1111 1111
~0 = 1111 1111
Negative zero is the condition where all bits in a signed word are on (1). This follows the 1's complement rules that a value is negative when the leftmost bit is 1, and that a negative number is the bit complement of the number's magnitude. The value also behaves as zero when computing. Adding or subtracting negative zero to/from another value produces the original value.
Adding negative zero:
0001 0110 22
+ 1111 1111 −0
=========== ====
1 0001 0101 21 An endaround carry is produced.
+ 0000 0001 1
=========== ====
0001 0110 22 The correct result (22 + (−0) = 22)
Subtracting negative zero:
0001 0110 22
− 1111 1111 −0
=========== ====
1 0001 0111 23 An endaround borrow is produced.
 0000 0001 1
=========== ====
0001 0110 22 The correct result (22 − (−0) = 22)
XOR
A bitwise exclusive OR takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 OR only the second bit is 1, but will be 0 if both are 1 or both are 0. This is equivalent to being 1 if the two bits are different, and 0 if they are the same. For example:
0101 (decimal 5)
XOR 0011 (decimal 3)
= 0110 (decimal 6)
Reference 
http://academic.evergreen.edu/projects/biophysics/technotes/misc/bin_math.htm
http://en.wikipedia.org/wiki/Ones%27_complement
http://en.wikipedia.org/wiki/Two's_complement
http://en.wikipedia.org/wiki/Bitwise_operation
No comments:
Post a Comment