representing data. representing data u the basic unit of memory is the bit a transistor that can...
TRANSCRIPT
Representing Data
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?
Representing numbers
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
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
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
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
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
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
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
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
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
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
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
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
Binary-Hex Conversion Example
Convert the binary number 1101010100101010011 to hexadecimal
1101010100101010011
0
6 A 9 5 3
Representing Colors and Strings
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!
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
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!
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
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
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
Color Intensity
00 is the lowest intensity FF is the highest intensity What color is &H000000? What color is &HFFFFFF?
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
Representing Text
ASCII table (starting from #32)
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