Download DataRepresentations

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Data Representations
Data: the information which a computer processes.
All data is stored and processed in binary form.
A bit is a binary digit, smallest unit which can be stored in computer.
A byte is grouped bits.
1 bytes = 8 bits
A word defines the number of bytes which can be stored in a memory cell.
Data types in a computer
 Text (individual characters and strings).
 Integers.
 Floating point numbers (numbers w/ both integer and fractional part).
 Boolean or logical symbols (two possible values: true and false).
 Special symbols(program instructions and other special codes unique to the
program)
Textual data
 Characters
o Upper/Low case letters (26 each, case sensitive)
o Digits (10)
o Punctuations (32)
o Other special symbols
 A coding system must be selected that assign each character its own binary
code.
 ASCII (American Standard Code for Information Interchange) (Pg.
637): represents each character in a 7-bit format. 27=128 characters.
Hello  1001000 1000101 1001100 1001100 1001111
Most systems use an extended 8-bit version (0 means standard ASCII
character, 1 means no-standard).
 EBCDIC (Extended Binary Coded Decimal Interchange Code): a 8-bit
coding system. Only used for IBM mainframe machine. 28=256
characters.
 Unicode: a 16-bit coding system. The internationally standard code is
a 16-bit code which allows for as many as 216=65,536 characters.
Actually, to date, only about 60% (38,887) of the available codes have
been assigned.
The ASCII code is essentially a subset of this code (just put eight 0’s
before each extended ASCII code and you get the corresponding
Unicode)
Integer


Unsigned (no-negative): numbers are stored in their binary representation
padded on the left with 0.
Eg. 0 0 0 0 0 1 1 0
+6 (8-bit memory)
Singed.



o The leftmost bit, known as the high-order bit, represents the sign of the
number.
Eg. 0 0 0 0 0 1 1 0
+6
10000110
-6
Size: n-bit memory, 2n.
Eg. In 8-bit memory, 28 = 256.
In 16-bit memory, 216 = 65536.
Range
o Unsigned: n-bit memory, 0 ~ 2n – 1.
Eg. In 8-bit memory, 0~28-1 = 0~255.
In 16-bit memory, 0~216-1 = 0 ~ 65535.
o Signed: n-bit memory, -2n-1 ~ 2n-1–1
Eg. In 8-bit memory, -27 ~ 27-1 = -128 ~ 127.
In 16-bit memory, -215 ~ 215-1 = -32768 ~ 32767.
Signed integers only have half the absolute magnitude of their
unsigned relatives.
Question: +0 and -0? (+0: 00000000 -0: 100000000)
Solution: in general, negative numbers are represented using the 2’s
complement.
Complement notation
 One’s Complement. The complement of a binary number is its bit string
written with each digit reversed. That is, each 1 in the bit string is written as a
0, and each 0 becomes as a 1.
Eg.
The one’s complement of the bit string 1 0 0 0 is 0 1 1 1.
The one’s complement of the bit string 1 1 0 0 1 1 0 0 is 00110011?




Two’s Complement. The two’s complement of a binary number is defined as
the one’s complement of the number +1.
Eg.
The two’s complement of the binary number 0 0 0 0 0 1 1 0 (6) is:
11111001
+
1
1 1 1 1 1 0 1 0 (-6)
It only represents negative numbers. If positive, conversion conforms to the
ordinary way.
Converting a negative decimal to 2’s complement binary (Eg. -1)
1. List the ordinary binary string for its positive relative. +1  001.
2. Covert 1 to 1’s complement. 001  110
3. Add 1. 110 + 1 = 111. (111 is 2’s complement for -1)
Converting a 2’s complement binary to a negative decimal
If n = number of bits that negative number correspondent to
binary value – 2n.
3
Eg. 111 = 7 – 2 = 7 – 8 = -1
Exercise:
Ordinary
Binary
000
001
010
011
100
Decimal
0
1
2
3
4
2’s complement
Binary
000
001
010
011
100
101
110
111
5
6
7
101
110
111
Decimal
0
1
2
3
-4 (22-23=48= -4)
-3 (5-23=-3)
-2 (6-23=-2)
-1 (7-23=-1)
Important notes about 2’s complement
 Leftmost bit indicates sign.
Eg. 001 = +1
101 = -3 (you may not be able to tell the value, but you at least can tell
it is negative)
 Negative number cannot be read as standard binary numbers.
Eg. The bit string 0 1 1 1 1 1 1 1 represents the decimal number 32767. If the
high-order bit were set to 1, i.e. the bit string turns to be 1 1 1 1 1 1 1 1 , the
number would be interpreted as -1?
 Range of values is split in half: -2n-1 ~ 2n-1–1
o Eg. The largest positive number in 3-bit memory is 23-1–1 = 4–1 = +3,
in 4-bit memory is +7.
 Add leftmost zero for positive number is necessary.
Overflow
 Value too large for defined memory words.
0111 = +7
0010 = +2
------------1001 = 9-24=9-16= -7 (you suppose the sum equals +9, but you get -7)
 Typically program continues to run but incorrect results.
 Solution:
o Add bits.
o The programmer is responsible for not overflow.
Exercise: HW1 13a, 13b, 14a, 14c.
216
215
214
213
212
211 210 29 28
27 26 25 24 23 22 21 20
65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1