Last Updated: 2015-09-14 Mon 09:22

CS 100 HW 1: Bits and Algorithms

Table of Contents

1 Instructions

  • Complete the problems in each section below which is labelled Problem
  • This is an individual assignment: you must do your own work and may not share or compare answers with other students. If you are struggling, ask questions on Piazza, seek help from the TA or professor, and review the class notes and tutorial links provided.
  • Do your work in an acceptable electronic format which are limited to the following
    • A Microsoft Word Document (.doc or .docx extension)
    • A PDF (portable document format, .pdf)
    • HTML (.html or .htm)
    • Plain text (.txt)

    You may work in other programs (Apple Words, Google Docs, etc.) but make sure you can export/"save as" your work to an acceptable format before submitting it.

  • Submit your work to our Blackboard site.
    • Log into Blackboard
    • Click on CS 100->HW Assignments->HW 1
    • Press the button marked "Attach File: Browse My Computer"
    • Select your file
    • Click submit on Blackboard

2 Problem 1: Babylonian Algorithm Demonstration

Recall the Babylonian Algorithm for calculating a square root that we discussed in class. It works as follows.

  • \(S\) is an input number, the algorithm calculates \(\sqrt{S}\) (square root of \(S\))
  • Initialize: Set \(x\) to a guess
  • Repeat
    • Calculate \(x_{next} = \frac{1}{2}\left(x + \frac{S}{x}\right)\)
    • Set \(x\) to be \(x_{next}\)
    • If \(x^2\) is close enough to \(S\), quit

Demonstrate 3 Iterations of the Babylonian Algorithm. Use the Babylonian Algorithm to calculate an approximation for the square root of 7 which is \[\sqrt{7} \approx 2.6457513110645907 \]

  • Use an initial guess of 3
  • Perform the "Repeat" section 3 times
  • Feel free to use a calculator to help with addition/subtraction
  • Show the results you compute at each step
  • Include after each step the calculation how close \(x^2\) is to 7 which gives an indication of how close one is to the true answer

What to put in your HW Write-up

  • Your demonstration of 3 iterations of the Babylonian algorithm

3 Problem 2: How Many Iterations

An iteration is usually a full trip through a "repeated" part of an algorithm. A single iteration of the Babylonian algorithm is to do the steps

  • Calculate \(x_{next} = \frac{1}{2}\left(x + \frac{S}{x}\right)\)
  • Set \(x\) to be \(x_{next}\)
  • If \(x^2\) is close enough to \(S\), quit

Suppose we are interested in getting an error of less 0.01 when computing the \(\sqrt{23}\).

  • Start with a "bad" initial guess of \(x=3\)
  • How many iterations of the repeated loop does it take before \(|x^2 - 23| \leq 0.01\)?
    • The vertical bars mean "absolute value" here so negative numbers become positive numbers: \(|-1.23|\) is \(1.23\) and \(|0.024|\) is \(0.24\).
  • Show each step of your work as you did in Problem 1: Babylonian Algorithm Demonstration

What to put in your HW Write-up

  • Your demonstration of the Babylonian algorithm for the inputs above.
  • Describe in bold how many iterations it takes for the algorithm to get an accurate answer

4 Problem 3: Bits for Numbers, Converting Between Bases

Convert the following numbers from Base 2 to base 10. For full credit, show some of your work for each number.

  • 100112: convert to base 10
  • 1110112: convert to base 10
  • 001102: convert to base 10

This tutorial gives you several ways of converting decimal numbers to binary. I tend to favor the first method which involves repeatedly dividing by two and keeping track of remainders.

Convert the following base 10 numbers to base 2

  • 1010: convert to base 2
  • 4510: convert to base 2
  • 12810: convert to base 2

What to put in your HW Write-up

  • Your answers to the 3 binary to decimal conversion problems above
  • Your answers to the 3 decimal to binary conversion problems above
  • Show your work to receive full credit

5 Bits for Color: Hexadecimal Numbers for Color

Bits are usually crammed together to representing lots of things. A common one on the web is to represent colors. Usually these show up as a hexadecimal color code such as those that appear in a color picker.

Hexadecimal is a base 16 representation for colors. It can be understood by breaking the number into pieces for each of the primary colors and looking at their bits.

Hexadecimal: #010297
               R G B

By parts:     01    02   97
             Red Green Blue

As Hex:       0    1    0    2    9    7
As bits:   0000 0001 0000 0010 1001 0111
                 Red     Green      Blue

The color above is mostly blue with just a hint of red and green. Each of the numbers associated with the color indicate how much red, green, or blue to mix in to get the final color.

Hexadecimal is a base 16 system in which 4 bits are represented by a single character according to the following conversion table.

Decimal Hex Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111

Red gets 8 bits (which is a byte) so it is represented using 2 hexadecimal digits such as 19, 0E, F7 or AB. Each of these can be translated a series of bits or a decimal number. The are in turn

Hex Decimal Bits
19 1*16 + 9 = 25 0001 1001
0E 0*16 + 14 = 14 0000 1110
F7 15*16 + 7 = 247 1111 0111
AB 10*16 + 11 = 171 1010 1011

Each of Red, Green, and Blue gets a byte (8 bits) to represent its level in a hexadecimal color code.

6 Problem 4: What Colors?

Use a in a color picker to determine what the following colors look like.

  1. Describe how the color appears to you
  2. Describe the levels of Red, Green, and Blue each as "high", "medium", "low", "very low", etc.
  3. Show what bits of each of the Red, Green, and Blue portions of the color look like.

The colors are as follows.

  • 33CCFF: What color is it, what are its RGB levels, what is its bit representation?
  • 663310: What color is it, what are its RGB levels, what is its bit representation?
  • 129837: What color is it, what are its RGB levels, what is its bit representation?
  • BBBBBB: What color is it, what are its RGB levels, what is its bit representation?

What to put in your HW Write-up

  • Descriptions of the colors and the bit representations for each of the HEX color indicated above

7 Character Coding Schemes

As we will discuss in class, bits are often used to represent letters in some alphabet, termed a character code. The most common character code is the ASCII code which is focused mainly on English speakers. Here we will determine the number of bits required to represent some other alphabets of languages around the world.

As a starting example, consider the Latin (English) character set which is A to Z (26 upper-case characters) and a-z (26 lower-case characters). How many bits would be required just to represent these characters?

A sequence of N bits can represent up to 2N distinct objects. So we can count up.

  • With 1 bit we can represent 21 = 2 objects
  • With 2 bits we can represent 22 = 4 objects
  • With 3 bits we can represent 23 = 8 objects
  • With 4 bits we can represent 24 = 16 objects
  • With 5 bits we can represent 25 = 32 objects
  • With 6 bits we can represent 26 = 64 objects

There are 26 lower case plus 26 upper case characters for a total of 52 objects. With 5 bits, we could get lower or upper case, but to represet both we'll need to use 6 bits which can represent up to 64 different objects.

8 Problem 5: Combined Greek and Latin Alphabet

The Greek alphabet has 24 lower-case characters and 24 upper-case characters.

Suppose we want a character encoding which can represent both English and Greek characters in a mixture such as

I am the α and the Ω. -God

But, for mine own part, it was Ελληνες to me. -Shakespeare

What is the minimum number of bits required to encode the combined Greek and Latin characters? Describe how you arrived at your answer.

What to put in your HW Write-up

  • Your answer to the number of bits required to encode both Greek and Latin alphabets and an explanation of how you arrived at that number.

9 Problem 6: French Alphabet

Consider the French alphabet. It has the same characters as the latin alphabet (A-Z, a-z) but any character can have added to it one of 5 accents. The accents change how a word is to be pronounced.

Name Symbol Example
acute ' é
grave ` à, è, ù
circumflex ^ â, ê, î, ô, û
diaresis .. ë, ï, ü, ÿ
cedilla ç ç
normal none a, e, u

Assume any of character can have one of the five accents above or be an unaccented character (six total accent types). This isn't actually how french works, but the assumption simplifies the problem. Answer the following questions.

  • How many different characters must be represented in the French alphabet?
  • What is the minimum number of bits required to do so?
  • Make sure to describe how you arrived at both of your answers.

What to put in your HW Write-up

  • Your answer to the number of bits required to encode the French alphabet and an explanation of how you arrived at that number.

10 Further Reading on Coding Schemes

ASCII was one of the first wide-spread codes in use. It represents the latin alphabet, numbers, some punctuation, and some control characters using 7 bits per character for 128 total characters.

Unicode and it's implementation UTF-8 is a newer code that is friendier to non-english languages: it encodes 1,112,064 different characters such as 難 (Kanji) ௵ (Tamil) and ☻ (smiley). Not all characters get the same number of bits.

11 Problem 6: A Simple Processor

A simple desk calculator does operations only on integers (numbers without a fractional part).

The calculator stores numbers in two memory slot.

  • Screen Number: One number can be in the screen memory slot so that it is displayed on the screen
  • Background Number: One other number can be in the background memory slot which is not displayed

The calculator can perform any of the following functions

  1. Clear Screen: Set the Screen Number to be 0
  2. Clear Background: Set the Background Number to be 0
  3. Background to Screen: Move the Background Number to the Screen Number so that it can seen.
  4. Sign Swap: Change the sign of the Screen Number; if it is positive make it negative; if it is negative make it positive.
  5. Add Numbers: Add the Screen Number onto the Background Number and set both Screen and Background Numbers to be the result
  6. Subtract Numbers: Subtract the Screen Number from the Background Number and set both Screen and Background Numbers to be the result
  7. Multiply Numbers: Multiply the Screen Number by the Background Number and set both Screen and Background Numbers to be the result
  8. Divide Numbers: Divide the Background Number by the Screen Number and set both Screen and Background Numbers to be the result. Discard any remainders from the result.
  9. Update the Screen Number if a user presses 0,1,2,…,8,9 so that the screen number is different
    • Update 0: The user presses 0, multiply the Screen number by 10 and add 0
    • Update 1: The user presses 1, multiply the Screen number by 10 and add 1
    • Update 2: The user presses 2, multiply the Screen number by 10 and add 2
    • Update 9: The user presses 9, multiply the Screen number by 10 and add 9

Do the following to finish the problem

  • Draw a rough picture of what this calculator might look like on the outside (where the buttons and screen are located). Make sure to be clear about which buttons perform which operations. Include your drawing in your electronic submission.
  • How many different actions/instructions can be performed by the calculator? Make sure to count each kind of update as a separate instruction.
  • What is the minimum number of bits required to represent each instruction distinctly?

What to put in your HW Write-up

  • Your picture of the calculator, description of how many operations it has, and how many bits are required to encode those operations.

Author: Chris Kauffman (kauffman@cs.gmu.edu)
Date: 2015-09-14 Mon 09:22