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 container that can hold 1 or 0 All data is represented using 1’s and 0’s u How are integers represented? u How are colors represented? u How are strings represented?
Representing numbers
Weighted Positional Notation u Use the position of the symbol to indicate the value u 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 d 0,d 1,…,d n-1 d n-1.10 n-1 + … + d d
Weighted Positional Notation u In general, given a sequence of n digits s 0,s 1,…,s n-1 and a base b, s n-1.b n-1 + … + s 1.b 1 + s 0.b 0 yields a unique integer N u s 0 is the least significant digit u s n-1 is the most significant digit u The most significant symbol can not be zero
Weighted Positional Notation u Any positive integer value can be used as the base for a weighted positional notation u For bases less than or equal to 10, a subset of the ten decimal digits can be used binary (base 2) octal (base 8) u 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 u 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 u In VB, hexadecimal representation begins with &H &H10 (= decimal 16) u So the following instructions are identical form.backcolor = &H13 form.backcolor = 19
Representations in Different Bases binary hexdecimalbinaryhexdecimal A B C D E F15
Binary and Hexadecimal u Discriminating between two voltage levels is currently cheaper in digital circuitry than three or more, thus binary is the representation for computers u It is much easier for humans to work with hexadecimal representations Long strings of 1’s and 0’s are hard to remember u 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 s n-1... s 1 s 0 convert the sequence into a decimal number N using: N = s n-1 *b n-1 + … + s 1 *b 1 + s 0 *b 0
Conversion to Decimal Convert to decimal N = 1* * * * * *2 0 = 38 Convert 3b2 16 to decimal N = 3* * *16 0 = 946 Convert to decimal N = 3* * *8 0 = = 248
A Binary Exercise u The binary number is identical to 139 base 10 u In the decimal, the number 139 can be written is interpreted as: 1 X 100 = X 10 = 30 9 X 1 = + 9 Total 139 u In binary, the same procedure yields: 1 x 128 = 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: x 128 = 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 u 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 hexdecimalbinaryhexdecimal A B C D E F15
Binary-Hex Conversion Example u Convert the binary number to hexadecimal A 9 5 3
Representing Colors and Strings
Specifying Visual Basic Colors u 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 u All colors are a combination of three primary colors Red Green Blue u Specify intensity of each primary color to create a unique color
Combining primary colors 256 Intensities 256 Intensities 256 Intensities Total possibilities = or approximately 16.7 million colors!
Why 256 Intensities? u Computers operate on Base 2 u 1 bit can be either 0 or 1 0 is off, 1 is on u 1 byte consists of 8 bits Ex: The possibilities are 2 8 or u A byte is the smallest addressable unit of computer storage
Using hexadecimal for colors u Each primary color intensity is expressed as two hexadecimal digits u Possibilities are u 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 u 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 u 00 is the lowest intensity u FF is the highest intensity u What color is &H000000? u 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 , for e is So the message “Hello.” can be coded as
Representing Text ASCII table (starting from #32)
ASCII Table u The ASCII table is appendix A of the text u You can find many examples of the ASCII table of characters online, for example