Professional Documents
Culture Documents
Class Exercise
1. 2. 3. 4. 5. 6. Bin to dec: 1001 0011 Dec to bin: 105 Bin to hex: 0010 1110 1000 1011 Hex to bin: 0xFEDC Hex to dec: 0x10A Dec to hex: 165
Unsigned Integers
Represents positive integers only Not necessary to indicate a sign, so all bits can be used for the magnitude:
1 byte = 8 bits = 28 = 256 (0 to 255)
255 decimal = 1111 1111 binary
Signed Integers
Represents positive and negative integers MSB (Most Significant Bit leftmost bit) used to indicate sign
0 = positive, 1 = negative
One less bit is used for the magnitude, with one extra negative value
1 byte = 8-1 bits = 27 (-128 to +127)
-128 decimal = 1000 0000 binary -1 decimal = 1111 1111 binary 127 decimal = 0111 1111 binary
2 bytes = 16 bits = 216 = 65,536 (0 to 65,535) 4 bytes = 32 bits = 232 = 4,294,967,296 (0 to 4,294,967,295)
3
2 bytes = 16-1 bits = 215 (-32,768 to +32,767) 4 bytes = 32-1 bits = 231 (-2,147,483,648 to +2,147,483,647 )
9/23/2012
1s & 2s Complement
1s complement form
Formed by reversing (complementing) each bit
2s complement form
Formed by adding 1 to 1's complement Negative numbers are stored this way Additive inverse of a number Computer never has to subtract
A B = A + (-B)
5
Signed Integers
If MSB = 0, convert directly to decimal If MSB = 1, convert to 2's complement form (reverse the bits & add 1), then to decimal form
Class Exercise
Convert the following negative decimal numbers to 8 bit binary using the 2s complement
-1, -3, -8, -127
Convert to decimal
1. 1111 0111b = -9 2. 0000 1000b +1b 0000 1001b = +9
Example: -32
1. 0010 0000b = +32 2. 1101 1111b +1b 1110 0000b = -32
Convert to decimal
1. 1110 0000b = -32 2. 0001 1111b +1b 0010 0000b = +32
9/23/2012
Class Exercise
Evaluate 32 + (- 5) in 8 bit binary
Convert 32 to binary Convert -5 to 2s complement Add together Check your answer by converting back to decimal
10
Alphanumeric Data
Alphanumeric data such as names and addresses are represented by assigning a unique binary code or sequence of bits to represent each character. As each character is entered from a keyboard (or other input device) it is converted into a binary code. Character code sets contain two types of characters:
Printable (normal characters) Non-printable. Characters used as control codes.
CTRL G (beep) CTRL Z (end of file)
11
Alphanumeric Codes
There are 3 main coding methods in use:
ASCII EBCDIC Unicode
12
9/23/2012
ASCII
7-bit code (128 characters) has an extended 8-bit version used on PCs and non-IBM mainframes widely used to transfer data from one computer to another Examples:
od -c od -h
13
14
EBCDIC
An 8-bit code (256 characters) Different collating sequence to ASCII used on mainframe IBM machine Both ASCII and EBCDIC are 8 bit codes inadequate for representing all international characters
Some European characters Most non-Alphabetic languages eg Mandarin, Kanji, Arabic, etc
Unicode
New 16 bit standard - can represent 65,536 characters Of which 49,000 have been defined
6400 reserved for private use 10,000 for future expansions
15
16
9/23/2012
Character Representation
ASCII
American Standard Code for Information Interchange Standard encoding scheme used to represent characters in binary format on computers 7-bit encoding, so 128 characters can be represented 0 to 31 (& 127) are "control characters" (cannot print)
Ctrl-A or ^A is 1, ^B is 2, etc. Used for screen formatting & data communication
Numeric Data
Need to perform computations Need to represent only numbers Using ASCII coded digits is very inefficient
19
9/23/2012
Integer Representation
UNSIGNED representing numbers from 0 upwards or SIGNED to allow for negatives. In the computer we only have binary digits, so to represent negative integers we need some sort of convention. Four conventions in use for representing negative integers are:
Sign Magnitude 1s Complement 2s Complement Excess 128
21
Disadvantages
arithmetic is difficult Two representations for zero
00000000 10000000
22
Binary Arithmetic
Addition Table
Digit 0 1 0 1 Digit 0 0 1 1 Sum 0 1 1 0 Carry 0 0 0 1
Computers generally use a system called complementary representation to store negative integers. Two basic types - ones and twos complement, of which 2s complement is the most widely used. The number range is split into two halves, to represent the positive and negative numbers. Negative numbers begin with 1, positive with 0.
23 24
9/23/2012
25
Add 1
11111001 +1 11111010 (2s complement of 6)
27
28
9/23/2012
29
30
Binary Fractions
The Binary Point
Digits on the left +ve powers of 2 Digits on the right ve powers of 2
0010110.101002 1 1/2 2 4 8 16
32 (0.5) 1/ 4 (0.25) 1/ 16 (0.125)
1/
32 (0.0625)
9/23/2012
Integer Overflow
Problem: word size is fixed, but addition can produce a result that is too large to fit in the number of bits available. This is called overflow. If two numbers of the same sign are added, but the result has the opposite sign then overflow has occurred Overflow can occur whether or not there is a carry Examples: 01000000 ( +64) 10000000 (-128) 01000001 ( +65) 11000000 ( -64) 10000001 (-127) 01000000 ( +64)
33 34
This same approach can be used for binary numbers. A number represented by
M*BE
can be stored in a binary word with three fields: Sign - plus or minus Mantissa M (often called the significand) Exponent E (includes exponent sign) The base B is generally 2 and need not be stored.
35
The more bits we use for the exponent, the larger the range of numbers available, but at the expense of precision. We still only have a total of 232 numbers that can be represented. A value from a calculation may have to be rounded to the nearest value that can be represented. Converting 5.75 to 32 bit IEEE format 5.75 (dec) = 101.11 (bin) +2 = +1.0111 * 2
36
9/23/2012
Overflow refers to values whose magnitude is too large to be represented. Underflow refers to numbers whose fractional magnitude is too small to be represented - they are then usually approximated by zero.
38
40
10
9/23/2012
Boolean Representation
Boolean or logical data type is used to represent only two values:
TRUE FALSE
Binary Data
Decimal, hex & character representations are easier for humans to understand; however All the data in the computer is binary An int is typically 32 binary digits
int y = 5; (y = 0x00000005;)
In computer y = 00000000 00000000 00000000 00000101
Although only one bit is needed, a single byte often used. It may be represented as:
0016 = FALSE FF16 or Non-Zero = TRUE
This data type is used with logical operators such as comparisons = > <
41
42
Binary Data
A char is typically 8 binary digits
char x = '5'; (or char x = 53, or char x = 0x35)
In computer, x = 00110101
Note that the ASCII character 5 has a different binary value than the numeral 5 Also note that 1 ASCII character = 2 hex numbers
43
11
9/23/2012
Bits, Bytes
1 bit 0 1
Bit Permutations
2 bits 00 01 10 11 3 bits 000 001 010 011 100 101 110 111 4 bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Symbol KB MB GB TB
Number of Bytes 210 = 1024 220 (over 1 million) 230 (over 1 billion) 240 (over 1 trillion)
RAM/ROM
Memory mapping Address Values 1 0 1 1 1 0 0 1 0000 0 0 0 0 1 1 1 1 0001 0002 .. .. .. 1 1 1 0 0 0 1 1
Size reminder:
Kilobyte Megabyte Gigabyte Terabyte Petabyte Kb Mb Gb Tb Pb 210 ~103 220 ~106 230 ~109 240 ~1012 250 ~1015
.. FFFF
0 0 1 0 0 1 0 0
47
12