CS074-Digital World
Review
Summary of Course Contents
Units of Information
- Understand the terms bit, byte, kilobyte, megabyte, gigabyte so
that you are able to translate easily between them.
- Understand that there are 2n different sequence of n
bits, and
all that this entails.
Binary Encoding of Numbers
- Understand how positional number systems work and how to convert
between representations at different bases, and how to do arithmetic at
different bases.
- Understand, in particular, representation at bases two (binary),
ten (decimal), and sixteen (hex); as well as conversion between these
bases.
Binary Encoding of Text
- Understand the fundamentals of ASCII. You don't need to memorize
the encodings of any characters, but you should understand that each
character is encoded by a single byte, that the digit characters '0' -
'9' have consecutive codes, likewise the upper-case letters 'A'-'Z' and
the lower-case letters 'a'-''z'.
- In particular, understand that a character string like "193" is
encoded by three bytes, and that the binary representation of the
number 193
as an integer is something entirely different.
- Be at least aware that there are other systems, like Unicode,
which uses two bytes to encode a character.
Binary Encoding of Images
- Understand the most straightforward way of encoding images:
24-bit bitmapped color images, in which each pixel is represented by a
three-byte RGB encoding of the color.
- Be able to solve simple problems in which some transformation of
the bytes of the representation results in a desired change in the
image.
- Be aware that there are other possibilities, especially
compressed formats like GIF and JPEG, although you don't need to know
the details of how these formats work.
Binary Encoding of Sound
- Understand the notion of sampling, and the structure of .wav
files, in which every sample is represented by a number (in our
examples in class, a single byte).
- Be able to solve simple problems in which some transformation of
the
bytes of the representation results in a desired change in the sound.
- Be aware that there are other possibilities, especially
compressed
formats like MP3, although you don't need to know the details
of how these formats work.
Compression Basics
- Understand the distinction between lossless and lossy
compression, and when one or the other is appropriate.
- Understand what "compression ratio" is.
- Understand the principles of lossless compression---redundancy in
information, and using methods like Huffman coding to reduce redundancy.
Python Programming
- Variables, assignment, simple arithmetic expressions.
- Basic data types: integer, floating point, and string.
Different meanings for + operator when applied to strings or numbers.
Different meanings for / operator when applied to integers or floating
point numbers.
- Arguments and return values in functions.
- for statement to loop over elements in a list or a string.
- relational operators (<,>,<=,>=,==,!=), logical
operators (and, or, not), and if, elif, else statements.
- random number generation
- strings
- lists
- Applications: Manipulating image files.
- Applications: Drawing on images.
- Applications: Getting information from the Web.
Internet Basics
- IP addresses
- Protocols for different internet services. You don't need
to know any details about protocols like SMTP and HTTP---just that
there is some standard format for the exchange of messages in each of
these.
- HTML--again, you don't have to know any HTML details, just to be
aware that Web pages are encoded as text files written in HTML.
Problems
1. 'Textese', or 'SMS language' refers to the style in which text
messages are written. Since people firing off text messages tend
to ignore capitalization and punctuation, and since line breaks are
handled automatically, textese in effect uses a character set
consisting of just 26 letters, the 10 digits, and a space.
Suppose we want to devise a binary encoding of this character
set.
(a) What is the least
number of bits required to
encode each character, assuming that we use the same number of bits for
each character?
(b) In the earliest days of SMS, text messages were
limited to 160 characters. Assuming you use the encoding of part
(a), what is the maximum number of bytes in such a text message?
2. Write in both decimal and hex the number whose binary
representation is 1101011.
3. Write two hundred thirty three in both binary and hex.
4. Describe the steps you would have to perform in the Binary Editor to
convert a color bitmap image into a grayscale negative. (Pictured
below.)

5. The steps you carried out in Problem 4 involve a certain operation,
or sequence of operations, applied to the bytes of a file.
Suppose you applied exactly the same operations to the audio samples in
a .wav file. What would be the effect on the sound?
6. This problem describes a data compression technique used to
compress black-and-white images, in which each pixel is either black or
white, and thus represented by a single bit. It is well suited for
things like fax messages that consist of text and simple line drawings.
(a) Just to get things started off, before we describe the compression
technique, how many bytes are required to encode a 400 pixel x 400
pixel image when each pixel is represented by a single bit.
The data compression method looks at each row of the image.
Suppose a row consist of 280 white pixels, followed by 3 black pixels,
followed by 74 white pixels, then 2 black pixels, then 41 white
pixels. This row will be encoded as the sequence of numbers
280,2,74,2,41. We will use two bytes for each number (why?)
Thus the number of bytes used to encode each row is twice the number of
alternations between black and white that you find as you scan along
the row. This compression method is called run-length encoding.
The full encoding of the image will contain the encoding of each row,
together with two bytes telling the number of pixels in each row.
(b) Is this a lossless or lossy compression method? Explain.
(c) Suppose that, on average, each row is encoded by five numbers, as
in our example above. What compression ratio is achieved in
encoding a 400x400 pixel image in this fashion?
(d) Which of these two images will be compressed more by this
method: A brief handwritten note (about 50 words) or a
typewritten page of text (about 200 words)? Explain.
7. What are the values of the variables x and y after the following
Python statements are executed?
x=7/2*3.5
y="boston"+str(x)
8. Write a python program that draws a house, but paints it in randomly
chosen colors. Thus you should get a different color scheme
each time you run the program. Here's a much trickier question:
Then try to rewrite the program in a fashion so that the difference
between the largest and smallest components of the color is at least
150---this should give you a highly saturated color.
9. Write a python program that opens an image file and displays a
grayscale negative of the image---essentially question 4 above, but now
you have to write it in python.
10. (a) The following python function convert
is meant to take an integer argument in the range 0 to 255 and returns
a string of length 2. What string is returned when the argument
is 97? What useful thing is this function doing in general?
What happens when you pass this function an argument outside the
prescribed range (like -2 or 256)?
def
convert(num):
code="0123456789ABCDEF"
result=code[num/16]+code[num%16]
return
result
(b) Write a python function that prompts the user to
enter an integer at the keyboard and then prints the value returned
when this integer is passed to convert as an argument. In other
words, your function should call convert. Your function should
also check to see if the integer is in the appropriate range, and print
an error message if it is not. You can use the built-in JES
function requestInteger: this
will check that the input really is an integer.
11. Write a python function that does the reverse of the function
convert in 8(a): This
should take a string of length 2 as an argument and return the
corresponding integer. (To do this, you really need to understand
the useful thing that convert
does.) There are several approaches to this problem. It can
be useful to know (a) you can compare characters using < (for
instance 'a'<'d' is true); (b) the ord function gives the ASCII code
of a character; (c) the chr function reverses the ord function, i.e.,
it returns the character whose ASCII code is the argument.