[8 pts] How many bits in a byte? How many bytes in a kilobyte of memory?
There are 8 bits in a byte and 1,024 (that is, 210) bytes in a kilobyte of memory.
[8 pts]
One of the lines we've been including in the head
section
of our XHTML files is the following.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
This line mentions UTF-8. What is UTF-8?
UTF-8 is a technique for encoding Unicode characters in such a way that the first 128 values are encoded as one byte per character. This allows the software that only recognizes ASCII characters to display files correctly for files that happen to contain only characters that appear in the ASCII character set.
[12 pts] Suppose we have the following HTML form, which displays two text fields and a button.
<form method="post" action="hyp.php"> <p>First leg: <input type="text" name="leg1" /></p> <p>Second leg: <input type="text" name="leg2" /></p> <p><input type="submit" value="Compute" /></p> </form>
Complete the following PHP file so that it computes the hypotenuse of a right triangle with the two specified lengths for the two legs surrounding the right angle. You can use the Pythagorean theorem, illustrated at right.
For example, if the user enters 3 and 4, hyp.php should display a response appearing as follows; note that the answer, 5, is in boldface text.
Hypotenuse
The hypotenuse is 5 units long.
If, however, the user gives a negative number or zero for one
or both of the legs
the program should display instead: Each leg must have
positive length.
<?php import_request_variables("pg", "form_"); ?> <html> <head> <title>Hypotenuse Computation</title> </head> <body>
<h1>Hypotenuse <p><?php if($form_leg1 <= 0 or $form_leg2 <= 0) { echo "Each leg must have positive length."; } else { $hyp = sqrt($form_leg1 * $form_leg1 + $form_leg2 * $form_leg2); echo "The hypotenuse is <b>$hyp</b> units long."; } ?></p> </body> </html>
[8 pts] Perform each of the following conversions.
a. | 11100010101100(2) to hexadecimal |
b. | 1EE7(16) to binary |
a. | 11100010101100(2) = | 38AC(16) |
b. | 1EE7(16) = | 1111011100111(2) |
[8 pts] How would of the following base-10 numbers be represented in an eight-bit two's-complement system?
a. | 0 |
b. | 108 |
c. | −108 |
a. | 0: | 00000000 |
b. | 108: | 01101100 |
c. | −108: | 10010100 |
[12 pts] Recall the Lempel-Ziv-Welch data compression algorithm.
1. Initialize table.
2. cur ← first character of file.
3. K ← next character of file.
4. Is cur + K in table?6. If characters remain in file, repeat from step 3.
Yes: cur ← cur + K. No: Add cur + K to table.
Output code for cur.
cur ← K
7. Output code for cur.
Suppose the table is initialized to have A mapped to 0, L mapped to 1, and F mapped to 2, and we're told to compress the string ALFALFA. What is the resulting code sequence? Show your intermediate work.
The code sequence is 0, 1, 2, 3, 5.
table | cur | K | output |
---|---|---|---|
A:0, L:1, F:2 | A | L | 0 |
A:0, L:1, F:2, AL:3 | L | F | 1 |
A:0, L:1, F:2, AL:3, LF:4 | F | A | 2 |
A:0, L:1, F:2, AL:3, LF:4, FA:5 | A | L | |
A:0, L:1, F:2, AL:3, LF:4, FA:5 | AL | F | 3 |
A:0, L:1, F:2, AL:3, LF:4, FA:5, ALF:6 | F | A | |
A:0, L:1, F:2, AL:3, LF:4, FA:5, ALF:6 | FA | 5 | |
[8 pts] Explain how the GIF file format provides support for including transparent pixels in an image.
The GIF format has a place for a bit specifying whether to
support transparency. If this bit is 1, then another byte in the
file identifies one of the
colors of the palette as the transparent
color.
[8 pts] How does the PNG format improve on the interlacing option available for GIFs?
GIF interlacing sends rows in interlaced order, but each row is sent in its entirety before proceeding to the next. With PNG interlacing, the image is divided into large blocks, and one pixel out of each block is sent first. In the beginning stages of receiving an interlaced PNG, then, only a fraction of the pixels in each row are received, so that an approximation of all the rows are received much earlier than with GIFs.
[8 pts] The first step with JPEG compression is to take each pixel's three RGB values (red, green, blue) and transform them into a different three values named Y, Cr, and Cb. Since each pixel still has three values, all still in the range of 0 to 255, this doesn't directly help with compressing. So why does this step exist in JPEG compression?
Human eyes are very sensitive to brightness (the Y value) but not nearly as sensitive to color at the same brightness level (the Cr and Cb values). As a result, a JPEG-compressed image can safely throw away a lot of the Cr and Cb information since human eyes won't easily be able to detect the loss anyway. In fact, JPEG-compressed images often include only one Cr and Cb value for each 4-pixel block in the image.