Integer
Representation
Introduction
-Representing decimal numbers as one character
per byte is
very wasteful
-Numbers are stored as binary with various
encodings
-With the standard conversion of decimal
integers to binary
integers, in one byte, the numbers 0 to 255 are
represented by
0000 0000 (2) = 0 (10) to 1111 1111 (2) = 255
(10)
where decimal numbers in parentheses indicate
the base
-To represent more numbers, use more bytes
-What about negative numbers? Use 1 bit for the
sign, gives
signed magnitude representation
Sign-magnitude representation
In computing, signed number representations are
required to encode negative numbers in binary number systems.
In mathematics, negative numbers in any base
are represented by prefixing them with a − sign. However, in computer hardware,
numbers are represented in bit vectors only without extra symbols. The four
best-known methods of extending the binary numeral system to represent signed
numbers are: sign-and-magnitude, ones' complement, two's complement, and
excess-K. Some of the alternative methods use implicit instead of explicit
signs, such as negative binary, using the base −2. Corresponding methods can be
devised for other bases, whether positive, negative, fractional, or other
elaborations on such themes. In practice the representation most generally used
in current computing devices is two's complement, although there is no
definitive criterion by which any of the representations is universally
superior.
In the first approach, the problem of representing
a number's sign can be to allocate one sign bit to represent the sign: set that
bit (often the most significant bit) to 0 for a positive number, and set to 1
for a negative number. The remaining bits in the number indicate the magnitude
(or absolute value). Hence in a byte with only 7 bits (apart from the sign
bit), the magnitude can range from 0000000 (0) to 1111111 (127). Thus you can
represent numbers from −12710 to +12710 once you add the sign bit (the eighth
bit). A consequence of this representation is that there are two ways to
represent zero, 00000000 (0) and 10000000 (−0). This way, −4310 encoded in an
eight-bit byte is 10101011.
This approach is directly comparable to the
common way of showing a sign (placing a "+" or "−" next to
the number's magnitude). Some early binary computers (e.g., IBM 7090) used this
representation, perhaps because of its natural relation to common usage.
Sign-and-magnitude is the most common way of representing the significand in
floating point values.
One Complement Representation
The ones' 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 vice-versa). The ones'
complement of the number then behaves like the negative of the original number
in most arithmetic operations.
A ones' complement system or ones' complement
arithmetic is a system in which negative numbers are represented by the
arithmetic negative of the value. In such a system, a number is negated (converted
from positive to negative or vice versa) by computing its ones' complement. An
N-bit ones' complement numeral system can only represent integers in the range
−(2N−1−1) to 2N−1−1 while two's complement can express −2N−1 to 2N−1−1.
The ones' complement binary numeral system is
characterized by the bit complement of any integer value being the arithmetic
negative of the value. That is, inverting all of the bits of a number (the
logical complement) produces the same result as subtracting the value from 0.
Two Complement Representation
Two's complement is a mathematical operation on
binary numbers, as well as a representation of signed binary numbers based on
this operation. The two's complement of an N-bit number is defined as the
complement with respect to 2N, in other words the result of subtracting the
number from 2N. This is also equivalent to taking the ones' complement and then
adding one, since the sum of a number and its ones' complement is all 1 bits.
The two's complement of a number behaves like the negative of the original
number in most arithmetic, and positive and negative numbers can coexist in a
natural way.
In two's-complement representation, negative
numbers are represented by the two's complement of their absolute value;[1] in
general, negation (reversing the sign) is performed by taking the two's
complement. This system is the most common method of representing signed
integers on computers.[2] An N-bit two's-complement numeral system can
represent every integer in the range −(2N−1) to +(2N−1 − 1) while ones'
complement can only represent integers in the range −(2N−1 − 1) to +(2N−1 − 1).
The two's-complement system has the advantage
that the fundamental arithmetic operations of addition, subtraction, and
multiplication are identical to those for unsigned binary numbers (as long as
the inputs are represented in the same number of bits and any overflow beyond
those bits is discarded from the result). This property makes the system both
simpler to implement and capable of easily handling higher precision
arithmetic. Also, zero has only a single representation, obviating the
subtleties associated with negative zero, which exists in ones'-complement
systems.
The method of complements can also be applied
in base-10 arithmetic, using ten's complements by analogy with two's
complements
Converting Between Different Bit Length
NUMBER SYSTEM CONVERSION
DECIMAL
|
BINARY
|
HEXADECIMAL
|
0
|
0000
|
0
|
1
|
0001
|
1
|
2
|
0010
|
2
|
3
|
0011
|
3
|
4
|
0100
|
4
|
5
|
0101
|
5
|
6
|
0110
|
6
|
7
|
0111
|
7
|
8
|
1000
|
8
|
9
|
1001
|
9
|
10
|
1010
|
A
|
11
|
1011
|
B
|
12
|
1100
|
C
|
13
|
1101
|
D
|
14
|
1110
|
E
|
15
|
1111
|
F
|
-Decimal and Binary-
Example1.1:
By adding together all the decimal
number values from right to left at the positions that are represented by a
"1" gives us: (256) + (64) + (32) + (4) + (1) = 35710 or
three hundred and fifty seven in decimal.
Then, the binary array of digits 1011001012 is
equivalent to 35710 in
decimal or denary. As the decimal number is a weighted number, converting
from decimal to binary will also produce a weighted binary number with the
right-hand most bit being the Least Significant Bit or LSB,
and the left-hand most bit being the Most Significant Bit or MSB.
and we can represent these as.
Example 1.2:
Convert the decimal number 29410 into
its binary number equivalent.
Then, the decimal to binary conversion
gives the decimal number 29410 equivalent
of 1001001102 in binary, reading from right to
left.
|
- Hexadecimal and binary-
To convert hexadecimal to binary, at each
hexadecimal digit we associate the 4 bits binary number using the following
table:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 110
E 1110
F 1111
Example 2.1:
Example 2.2:
1000100100110111 (a 16-bit Byte)
Break
the Byte into 'quartets' - 1000 1001
0011 1111
1000=8+0+0+0=8
1001=8+0+0+1=9
0011=0+0+2+1=3
1111=8+4+2+1=F
Therefore ... 1000100100110111 = 8937Hex |
Goh Pei Ing
B031210215
No comments:
Post a Comment