Data Representation Robin Burke IT 130
Outline Data Representation Binary Numbers Image types
Apology Errors The math in this chapter is full of errors
Number Systems A number system for n Digits from 0 to n-1 k th position = factor of n k Decimal (n=10) digits in position 2 = 1 * in position 1 = 5 * 10 4 in position 0 = 4 * 10 0 = 4 * 1 Hexadecimal (n=16) digits 0-9ABCDEF 3EA 3 in position 2 = 3 * 16 2 E in position 1 = 14 * 16 A in position 0 = 10 * 1 in decimal 1002
Babylonian (base 60)
Binary Numbers Binary (n = 2) digits 0 and 1 binary digit (bit) in position 3 = 1 * in position 2 = 1 * in position 0 = 1 * 1 in decimal 13
Why binary? Switches the computer is a bunch of intricately-connected switches transistors a switch is either off or on Early computers were actually programmed with switches early personal computers, too
An Early Home Computer
Memory Usually described in terms of kilobytes (KB) or megabytes (MB) 1 byte = 8 bits 1 KB = 2 10 or 1024 bytes 1 MB = 2 20 or 1,048,576 bytes 256 MB of RAM 268,435,456 bytes or 2,147,483,648 bits more than 2 billion transistors actually transistor / capacitor pair
16 million transistors in this L2 cache
Data Representation Everything in the computer is binary the program the operating system HTML files the values manipulated by JavaScript Part of what the computer does is translate from our representations to its representation = binary and back
Obvious Example Images More about images later
Integers Modern computers allocate 4 bytes (32 bits) to an integer 2 32 = 4.3 billion bit patterns but we need some for positive numbers and some for negative Basic idea give up one bit (highest-order) as a "sign" bit the problem
Two's complement Represent negative numbers in opposite order (not 2 -31, typo in book)
Integer representation 32 bits first bit = sign bit if sign bit = 0 interpret as binary number if sign bit = 1 flip all the other bits interpret as negative number What is ( ) + 1?
Floating point Think of scientific notation convert so that the decimal point has one digit in front of it x 10 3 since we know where the decimal point is three things to track + or – exponent of 10 value
Floating Point 32 bits sign bit 8 bits exponent 23 bits fractional part sign bit = 0 exponent = 3 = fraction = = Double-precision 64 bits 11 for exponent 52 for fraction more accuracy
Characters Historically 1 byte (8 bits) = 1 character 8 bits = 256 possible bit patterns Character set mapping between bit patterns and characters A = (65), a = (97) does not work for languages with larger character sets Unicode a character = 16 bits 65,536 characters
Characters ASCII the most common mapping used today A-Z = 65-91, a-z =
Strings Strings are lists (really arrays) of characters "foo" think of a list with 3 elements, each a character Not 100% accurate see on Wednesday
Data Representation Is determined by context what kind of variable is declared where the value came from Example
Images Complex topic actually Basic idea divide the picture into an evenly-spaced grid picture elements = pixels build a representation of the grid data turn into a file To display reverse the process to create picture
Monochrome (B/W) Each pixel 0 (on) or 1 (off)
Eight bit color 256 color palette associated with display 8 bit value indexes into palette
24-bit color 8 bits per color (RGB)
But Consider a simple image 288 x 200 pixels = pixels x 3 bytes (24 bits) / pixel = 172, 800 bytes At 3 K / sec 57 = 1 min All useful image formats use compression
Image formats Most common GIF JPEG PNG How they differ use of color type of compression availability
GIF Format Graphics Interchange Format.gif oldest uses repetitive pattern encoding Pluses compression is accurate, no loss of quality Minuses less compression possible image can only have 256 colors shading looks ugly Applicability good for line drawings animation is supported
JPEG Format Joint Photographic Experts Group.jpg average color values across pixels Pluses Level of compression is adjustable Supports 24-bit color Minuses Compression loses information
PNG Format Portable Net Graphics.png developed in web era Pluses has aspects of both GIF and JPG Minuses older browsers don't support it
Bottom line GIF line art unless there is continuous tone content JPG images compression advantages of JPG not apparent below 200x200 or so can often get away with high compression PNG consider using if your audience has the latest software
Rules of Thumb Speed Dial-up about 5 K / sec DSL / Cable about K / sec Don't make user wait more than 2-3 sec for homepage Images deeper in site can be bigger Note images are cached by browser won't be downloaded every time they appear navigation bars, buttons, etc.
Wednesday Strings Ch. 15