4.1 Binary Representations

advertisement
4.1 Binary
Representations
•
•
•
•
•
•
•
•
•
•
4.1 : 1/13
why binary?
useful powers of two
systematic enumeration of binary patterns
binary representation of whole numbers
conversion between binary and decimal numbers
binary addition and signed numbers
binary fractions
binary representation of floating point numbers
binary encoded decimal
binary representation of printed characters
Why Binary?
• two-state logic is simple to implement and rugged against noise
• the two states are created by two different voltages (e.g. +5 and
0), two degrees of magnetism, length of an electronic pulse,
amount of optical absorption, polarization of light, etc.
• mathematically the two states represent the binary digits (or
bits ) 0 and 1
• the electronic circuit that holds one bit is called a flip-flop
• binary representations can have any number of bits, the most
common being eight, where 8 bits = 1 byte
• numbers and characters are composed of one or more bytes
• information is stored in memory or disk as an addressable array
of bytes
• the electronic circuit that performs binary logic or arithmetic is
called a register
• register size varies with the computer chip, with the most
common being 32 and 64 (4 and 8 bytes)
4.1 : 2/13
Powers of Two
It is extremely convenient to memorize several powers of two.
20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
210 = 1024
Other powers of two can be computed using power rules.
216 = (26)(210) = 65,536
220 = (210)2 = 1,048,576
230 = (210)3 = 1,073,741,824
Some powers of two have names that are not exactly correct
210
216
220
230
4.1 : 3/13
is
is
is
is
called
called
called
called
a kilobyte
64 kilobytes (26×210)
a megabyte
a gigabyte
Binary Patterns
• binary patterns are used to hold logic information (true, false),
numeric values, and characters
• binary patterns are ordered so that the rightmost column varies
with every entry, the next left column every two entries, then
every four entries, etc.
• for n bits the number of unique binary patters is given by 2n
•below is a listing of the eight unique patterns that can be
created with 3 bits
000
001
010
011
100
101
110
111
4.1 : 4/13
there is no fixed meaning associated
with each combination - the programmer
assigns different meanings as required
Representation of Whole Numbers
• decimal values are determined by powers of ten
170 = 1×102 + 7×101 + 0×100
• binary values are determined by powers of two
170 = 10101010b =
1×27 + 0×26 + 1×25 + 0×24 + 1×23 + 0×22 + 1×21 + 0×20
• if the least significant bit is 0 the number is even, if 1 then odd
• the largest unsigned number that can be represented in binary
is given by 2bits - 1 (this is because the value zero requires one
combination!)
1
2
4
8
4.1 : 5/13
byte = 8 bits =
bytes = 16 bits
bytes = 32 bits
bytes = 64 bits
256 - 1 = 255
= 65,536 - 1 = 65,535
= 4,294,967,296 - 1 = 4,294,967,295
= 18,446,744,073,709,551,615
Conversion between Representations
binary-to-decimal
write number vertically and add
appropriate powers of two
example: 01101011b
0
0
1
64
1
32
0
0
1
08
0
0
1
02
1
01
107
4.1 : 6/13
decimal-to-binary
subtract the largest power of two
from the number and repeat
example: 182
182
-128
27
54
25
-32
22
-16
24
6
-4
22
2
21
-2
0
10110110b
Addition of Binary Integers
basic rules
0
0
1
1
+
+
+
+
0
1
0
1
=
=
=
=
0
1
1
10b (0 plus a carry)
a carry increments the basic rules (C = 1)
0
0
1
1
+
+
+
+
0
1
0
1
+
+
+
+
C
C
C
C
=
=
=
=
1
10b (0 plus another carry)
10b (0 plus another carry)
11b (1 plus another carry)
simple example
00111010b
00011100b
01010110b
4.1 : 7/13
58
28
86
Signed Integers
First method: the sign of a binary integer can be indicated by the
most significant bit (sign bit = 0 if positive and 1 if negative)
+58:
00111010b
-58:
10111010b
The sign bit approach is used with fractional binary numbers.
Second method: a negative number can be represented by the
twos-complement of the positive number - take the complement
of the positive number (0 → 1, 1 → 0), then add 1.
+58: 00111010b
comp: 11000101b
+1: 11000110b (-58)
-58: 11000110b
comp: 00111001b
+1: 00111010b (+58)
Twos complement negative integers give the correct answer for
adding any combination of positive and negative values. In both
methods the most significant bit is 1 for a negative value. Signed
integers cover the range -2bits-1 to +(2bits-1 - 1).
4.1 : 8/13
Examples of Signed Addition
values used in the following examples
+58: 00111010b
+28: 00011100b
-58: 11000110b
-28: 11100100b
+30:
-30:
example 1
+58: 00111010b
-28: 11100100b
+30: 1|00011110
example 2
-58: 11000110b
+28: 00011100b
-30: 11100010b
example 3
-58:
11000110b
-28:
11100100b
-86: 1|10101010b
example 4
+58: 00111010b
-58: 11000110b
0: 1|00000000b
00011110b
11100010b
Note that the carry beyond the most significant bit must be
discarded to obtain a correct answer (this is called the bit bucket!)
4.1 : 9/13
Binary Fractions
• fractions are written with a "binary point"
... 222120.2-12-22-3 ...
decimal
0.500
0.250
3.750
binary
0.100b
0.010b
11.110b
• some rational decimal numbers are irrational binary numbers
0.1 = 0.0 0011 0011 0011b ... infinitely repeating
• a binary number can be constructed as a fraction with a binary
point times a power of two (analogous to scientific notation)
200 = 1.1001b×2111b
4.1 : 10/13
(1.5625×128)
Storing Binary Scientific Notation
• A 32-bit format has a 23-bit fraction (value between 1 and 2) and
an 8-bit exponent. A sign bit is used for the fractional part, while
the exponent is offset by 127 to provide + and − values.
±1.fffffffffffffffffffffff×2eeeeeeee
• 200 decimal would be represented by the following 32 bits
0|10010000000000000000000|10000110
where the left bit denotes a positive value, the next 23 bits are the
fractional part after the binary point 1.f, and the last 8 bits are 7
offset by 127.
• the number of bits used for the fraction gives the precision,
223 = ~107
• the number of bits in the exponent gives the dynamic range,
2±127 = ~10±38
• a 64-bit format has ~15 decimal digits of precision and a dynamic
range of 10±307
4.1 : 11/13
Binary Encoded Decimal
• Intel chips are capable of performing direct, decimal arithmetic
using binary encoded decimal (BCD)
• in BCD each byte holds two decimal digits - 4 bits apiece
0
1
2
3
4
=
=
=
=
=
0000
0001
0010
0011
0100
5
6
7
8
9
=
=
=
=
=
0101
0110
0111
1000
1001
unused
unused
unused
unused
unused
unused
=
=
=
=
=
=
1010
1011
1100
1101
1110
1111
• although 4 bits are required to represent the 10 decimal digits,
this means there are 6/16 unused combinations
• BCD is not ordinarily used by commercial software because of
the 60% increase in required memory
• decimal arithmetic avoids the binary round-off problem and for
that reason is used by some specialty applications (e.g. financial)
• decimal arithmetic is also used for computations with integers
having more than 20 digits (e.g. encryption schemes)
4.1 : 12/13
Binary Encoded Characters
• the American Standard Code for Information Interchange
(ASCII) is the most common method of representing printed
characters in binary
• ASCII uses 8 bits, but only the first 128 combinations are
standardized - the upper 128 are defined by software and/or
printer vendors
• a few examples:
space
!
0
1
32
33
48
49
00100000
00100001
00110000
00110001
A
B
a
b
65
66
97
98
01000001
01000010
01100001
01100010
• Internet oriented languages, such as Java, use multiple bytes
to represent each character in a scheme called Unicode
• in a two-byte Unicode representation the first byte is the
character and the second byte the character set, yielding
~65,000 combinations
4.1 : 13/13
Download