<< Chapter < Page | Chapter >> Page > |
Unsigned integers are represented by a fixed number of bits (typically 8, 16, 32, and/or 64)
If an operation on bytes has a result outside this range, it will cause an ‘overflow’
The binary representation discussed above is a standard code for storing unsigned integer numbers. However, most computer applications use signed integers as well; i.e. the integers that may be either positive or negative.
In binary we can use one bit within a representation (usually the most significant or leading bit) to indicate either positive (0) or negative (1), and store the unsigned binary representation of the magnitude in the remaining bits.
However, for reasons of ease of design of circuits to do arithmetic on signed binary numbers (e.g. addition and subtraction), a more common representation scheme is used called two's complement. In this scheme, positive numbers are represented in binary, the same as for unsigned numbers. On the other hand, a negative number is represented by taking the binary representation of the magnitude:
Example
+4210 = 001010102
and so-4210 = 110101102
Example
Performing two's complement on the decimal 42 to get -42
Using a eight-bit representation
42= 00101010 Convert to binary
11010101 Complement the bits11010101 Add 1 to the complement
+ 00000001--------
11010110 Result is -42 in two's complement
Addition and subtraction of unsigned binary numbers
Binary Addition is much like normal everyday (decimal) addition, except that it carries on a value 2 instead of value 10.
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0, and carry 1 to the next more significant bit
Example
00011010 + 00001100 = 00100110
1 1 carries0 0 0 1 1 0 1 0 = 26(base 10)
+ 0 0 0 0 1 1 0 0 = 12(base 10)----------------
0 0 1 0 0 1 1 0 = 38(base 10)11010001 + 00111110 = 100011010
1 1 1 carries1 1 0 1 0 0 0 1 = 208 (base 10)
+ 0 1 0 0 1 0 0 1 = 73 (base 10)----------------
1 0 0 0 1 1 0 1 0 = 281 (base 10)
The result exceeds the magnitude which can be represented with 8 bits. This is an overflow .
Subtraction is executed by using two's complement
Addition and subtraction of signed binary numbers
Binary Multiplication
Multiplication in the binary system works the same way as in the decimal system:
0 x 0 = 0
0 x 1 = 0
1 x 0 = 0
1 x 1 = 1, and no carry or borrow bits
Example
00101001 × 00000110 = 11110110
0 0 1 0 1 0 0 1 = 41(base 10)× 0 0 0 0 0 1 1 0 = 6(base 10)
----------------------0 0 0 0 0 0 0
0 1 0 1 0 0 10 1 0 1 0 0 1
----------------------------0 0 1 1 1 1 0 1 1 0 = 246(base 10)
00010111 × 00000011 = 010001010 0 0 1 0 1 1 1 = 23(base 10)× 0 0 0 0 0 0 1 1 = 3(base 10)
----------------------1 1 1 1 1 carries
0 0 1 0 1 1 10 0 1 0 1 1 1
0 0 1 0 0 0 1 0 1 = 69(base 10)
Notification Switch
Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?