representing data. representing data u the basic unit of memory is the bit a transistor that can...

27
Representing Data

Upload: mervyn-tyler

Post on 30-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representing Data

Page 2: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representing data

The basic unit of memory is the bit A transistor that can hold either high or low voltage Conceptually, a tiny container that can hold 1 or 0 All data is represented using 1’s and 0’s

How are integers represented? How are colors represented? How are strings represented?

Page 3: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representing numbers

Page 4: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Weighted Positional Notation

Use the position of the symbol to indicate the value By assigning each position the appropriate power

of the base, we can get a unique representation of numbers in that base

Decimal System (Base 10)Given a sequence of n digits d0,d1,…,dn-1

dn-1.10n-1 + … + d1.101 + d0.100

Page 5: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Weighted Positional Notation

In general, given a sequence of n digits s0,s1,…,sn-1

and a base b,

sn-1.bn-1 + … + s1.b1 + s0.b0

yields a unique integer N s0 is the least significant digit sn-1 is the most significant digit The most significant symbol can not be zero

Page 6: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Weighted Positional Notation

Any positive integer value can be used as the base for a weighted positional notation

For bases less than or equal to 10, a subset of the ten decimal digits can be used binary (base 2) octal (base 8)

For bases more than 10, new symbols will have to be introduced hexadecimal (base16)

A=10, B=11, C=12, D=13, E=14, F=15

Page 7: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Weighted Positional Notation

A subscript is used to indicate the base of the number in typeset A number without a subscript is assumed to be base 10 Not an option while programming

In VB, hexadecimal representation begins with &H &H10 (= decimal 16)

So the following instructions are identical form.backcolor = &H13 form.backcolor = 19

Page 8: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representations in Different Bases

binary hex decimal binary hex decimal 00000 0 1000 8 8 00011 1 1001 9 9 00102 2 1010 A 10 00113 3 1011 B 11 01004 4 1100 C 12 01015 5 1101 D 13 01106 6 1110 E 14 01117 7 1111 F 15

Page 9: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Binary and Hexadecimal

Discriminating between two voltage levels is currently cheaper in digital circuitry than three or more, thus binary is the representation for computers

It is much easier for humans to work with hexadecimal representations Long strings of 1’s and 0’s are hard to remember

Binary to hexadecimal conversion is more direct than binary to decimal conversion Reason: 16 is a power of 2

Page 10: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Conversion to Decimal

Given a sequence of base-r digits sn-1...s1s0

convert the sequence into a decimal number N using:

N = sn-1*bn-1 + … + s1*b1 + s0*b0

Page 11: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Conversion to Decimal

Convert 10 01102 to decimal

N = 1*25 + 0*24 + 0*23 + 1*22 + 1*21 + 0*20

= 38

Convert 3b216 to decimal

N = 3*162 + 11*161 + 2*160

= 946

Convert 3708 to decimal

N = 3*82 + 7*81 + 0*80 = 192 + 56 + 0

= 248

Page 12: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

A Binary Exercise The binary number 10001011

is identical to 139 base 10 In the decimal, the number

139 can be written is interpreted as:

1 X 100 = 100 3 X 10 = 30 9 X 1 = + 9 Total 139

In binary, the same procedure yields:

1 x 128 = 128 0 x 64 = 0 0 x 32 = 0 0 x 16 = 0 1 x 8 = 8 0 x 4 = 0 1 x 2 = 2 1 x 1 = + 1 Total = 139

Page 13: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

A Hexadecimal Exercise

Binary Decimal:

10001011

1 x 128 = 128 0 x 64 = 0 0 x 32 = 0 0 x 16 = 0 1 x 8 = 8 0 x 4 = 0 1 x 2 = 2 1 x 1 = + 1 Total = 139

Hexadecimal Decimal:

8B

8 x 16 = 128

B or 11 x 1 = +11

Total = 139

Page 14: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Converting Binary to Hexadecimal Converting binary to hexadecimal is a simple

exercise in pattern recognition Break the binary number into groups of 4 bits, starting

from the right If necessary, pad the leftmost group with 0's until it is

also 4 bits in length Replace each 4 bit grouping with a single hexadecimal

character determined by the chart

Page 15: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representations in Different Bases

binary hex decimal binary hexdecimal

00000 0 1000 8 8 00011 1 1001 9 9 00102 2 1010 A 10 00113 3 1011 B 11 01004 4 1100 C 12 01015 5 1101 D 13 01106 6 1110 E 14 01117 7 1111 F 15

Page 16: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Binary-Hex Conversion Example

Convert the binary number 1101010100101010011 to hexadecimal

1101010100101010011

0

6 A 9 5 3

Page 17: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representing Colors and Strings

Page 18: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Specifying Visual Basic Colors

Two ways: By Color name

Ex: “vbRed”, “vbGreen”, “vbBlue” Provides basic colors Must know the precise names allowed

By Hexadecimal color value Ex: “&H00FF3A” Allows up to 16.7 million possibilities!

Page 19: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Color Theory

All colors are a combination of three primary colors Red Green Blue

Specify intensity of each primary color to create a unique color

Page 20: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Combining primary colors

256 Intensities

256 Intensities

256 Intensities

Total possibilities =Total possibilities =2562563 3 or approximatelyor approximately16.7 million colors!16.7 million colors!

Page 21: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Why 256 Intensities?

Computers operate on Base 2 1 bit can be either 0 or 1

0 is off, 1 is on

1 byte consists of 8 bits Ex: 01011101 The possibilities are 28 or 0-255

A byte is the smallest addressable unit of computer storage

Page 22: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Using hexadecimal for colors

Each primary color intensity is expressed as two hexadecimal digits

Possibilities are 0-255 Ex: Convert 221 to Hexadecimal

221/16 = 13 Remainder 12 13 = D in Hexadecimal 12 = C in Hexadecimal 221 = &HDC

In the properties window, VB displays all color codes with 8 hexadecimal digits

• &H000000DC

Page 23: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Expressing hexadecimal colors

Sample Color Value:

&H00DC0F4A

Prefix indicating a hexadecimal number

The first byte codes the blue saturation

The second byte codes the green saturation

The third byte codes the red saturation

Page 24: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Color Intensity

00 is the lowest intensity FF is the highest intensity What color is &H000000? What color is &HFFFFFF?

Page 25: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representing Text

ASCII (American National Standards Institute ANSI)American Standard Code for Information Interchange

ASCII code is an 8bit code for character representation.

For example the code for H is 01001000, for e is 01100101So the message “Hello.” can be coded as

01001000 01100101 01101100 01101100 01101111 00101110

Page 26: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

Representing Text

ASCII table (starting from #32)

Page 27: Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny

ASCII Table

The ASCII table is appendix A of the text You can find many examples of the ASCII table of

characters online, for examplehttp://www.physics.udel.edu/~watson/scen103/ascii.html