Title: Representing Data
1Representing Data
2Representing 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 1s and 0s
- How are integers represented?
- How are colors represented?
- How are strings represented?
3Representing numbers
4Weighted 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
5Weighted 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
6Weighted 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)
- A10, B11, C12, D13, E14, F15
7Weighted 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
8Representations in Different Bases
- binary hex decimal binary hex decimal
- 0000 0 0 1000 8 8
- 0001 1 1 1001 9 9
- 0010 2 2 1010 A 10
- 0011 3 3 1011 B 11
- 0100 4 4 1100 C 12
- 0101 5 5 1101 D 13
- 0110 6 6 1110 E 14
- 0111 7 7 1111 F 15
9Binary 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 1s and 0s are hard to remember
- Binary to hexadecimal conversion is more direct
than binary to decimal conversion - Reason 16 is a power of 2
10Conversion to Decimal
- Given a sequence of base-r digits sn-1...s1s0
- convert the sequence into a decimal number N
using - N sn-1bn-1 s1b1 s0b0
11Conversion to Decimal
- Convert 10 01102 to decimal
- N 125 024 023 122 121 020
- 38
- Convert 3b216 to decimal
- N 3162 11161 2160
- 946
- Convert 3708 to decimal
- N 382 781 080 192 56 0
- 248
12A 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
13A 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
14Converting 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
15Representations in Different Bases
- binary hex decimal binary hex decimal
- 0000 0 0 1000 8 8
- 0001 1 1 1001 9 9
- 0010 2 2 1010 A 10
- 0011 3 3 1011 B 11
- 0100 4 4 1100 C 12
- 0101 5 5 1101 D 13
- 0110 6 6 1110 E 14
- 0111 7 7 1111 F 15
16Binary-Hex Conversion Example
- Convert the binary number 1101010100101010011 to
hexadecimal - 1101010100101010011
-
0
17Representing Colors and Strings
18Specifying 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!
19Color Theory
- All colors are a combination of three primary
colors - Red
- Green
- Blue
- Specify intensity of each primary color to create
a unique color
20Combining primary colors
256 Intensities
Total possibilities 2563 or approximately 16.7
million colors!
256 Intensities
256 Intensities
21Why 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
22Using 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
23Expressing 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
24Color Intensity
- 00 is the lowest intensity
- FF is the highest intensity
- What color is H000000?
- What color is HFFFFFF?
25Representing 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 01100101 So the message
Hello. can be coded as 01001000 01100101
01101100 01101100 01101111 00101110
26Representing Text
ASCII table (starting from 32)
27ASCII Table
- The ASCII table is appendix A of the text
- You can find many examples of the ASCII table of
characters online, for example - http//www.physics.udel.edu/watson/scen103/ascii.
html