Convert the decimal number 35 to binary. Solution 0 1 2 4 8 17 35
Binary 1 0 0 0 1 1
3.3
Integer
Representation
Range of Integers
An integer can be positive (正整數) or negative (負整數)
To use computer memory more efficiently, Integers can be represented as unsigned (無號:無正負號) or signed (有號:有正負號)numbers
There are three major methods of signed number representation:
Sign-and-magnitude (符號大小)
One’s complement (1的補數)
Two’s complement (2的補數)
Taxonomy of Integers
-8 = 1001000 11110111 11111000 8 = 00001000
Unsigned Integer
# of Bits ---------
8
16 Range-------------------------------------
0 ~ 255
0 ~ 65,535 Unsigned integer range: 0 … (2N-1)
Storing unsigned integers process:
The number is changed to binary
If the number of bits is less then N, 0s are to the left of the binary number so that there is a total of N bits
Example
Store 7 in an 8-bit memory location. Solution First change the number to binary 111.
Add five 0s to make a total of N(8) bits, 00000111.
The number is stored in the memory location.
Example
Store 258 in an 16-bit memory location. Solution First change the number to binary 100000010.
Add sevent 0s to make a total of N(16) bits, 0000000100000010.
The number is stored in the memory location.
Example of Storing Unsigned Integers in Two Different Computers
Decimal------------
7
234
258
24,760
1,245,678 8-bit allocation
------------
00000111
11101010
overflow
overflow
overflow 16-bit allocation
----------------------------
0000000000000111
0000000011101010
0000000100000010
0110000010111000
overflow Unsigned numbers are commonly used for
counting and addressing
Example
Interpret 00101011 in decimal if the number was stored as an unsigned integer. Solution Binary 0 0 1 0 1 0 1 1Weights 128 64 32 16 8 4 2 1
----------------------------------------- 0 + 0 + 32 + 0 + 8 + 0 + 2 + 1 Decimal 43
Sign-and-Magnitude Integers
# of Bits ----------
8
16
32 -127 -0
-32767 -0
-2,147,483,647 -0 +0 +127
+0 +32767
+0 +2,147,483,647 Range
------------------------------------------------------- Range: -(2N-1-1) … +(2N-1-1)
Storing sign-and-magnitude integers process:
The number is changed to binary; the sign is ignored
If the number of bits is less then N-1, 0 are add to the left of binary number so that there is a total of N-1 bits
If the number is positive, 0 is added to the left (to make it N bits). If the number is negative, 1 is added to thel left
Sign-and-Magnitude Integers
In sign-and-magnitude representation, the leftmost bit defines the sign of the number
If it is 0, the number is positive(正數)
If it is 1, the number is negative(負數)
Comments