CS 102
[Schedule]
[Examples] [Programs]
[Notes & Reference] [Syllabus]
[Lab & TA] [Tests]
[Grades]
How much do you want to remember?
The smallest unit of information a computer can store is a bit, represented as a 0 or a 1. The 0 and 1 values represent different voltages. A byte is a collection of 8 bits, and can be used for instance to represent a single character, such as 'A'. A word is typically a collection of two bytes, for a total of 16 bits.
456 = (4 * 100) + (5 * 10) + (6 * 1). Note the powers of ten (100, 10, 1) used in each place.
%1011 = (1 * 8) + ( 0 * 4) + (1 * 2) + (1 * 1) = 8 + 0 + 2 + 1 = 11 (decimal). Note the powers of two (8, 4, 2, 1) used in each place.
$9A = (9 * 16) + (A * 1). Remember that the symbol 'A' stands for the value 10, giving: (9 * 16) + (10 * 1) = 144 + 10 = 154 (decimal).
Let's say we want to convert %1010 to Decimal. Rewrite the binary number with the place values above each position as follows:
8 4 2 11 0 1 0This gives us powers of two (starting with the right-most value of 2 to the 0th power, or simply 2^0) on the top line, with the number itself on the bottom line. Wherever there is a one on the bottom line add the place value on the top line to your sum. This gives 8 + 2 = 10, which is the answer in decimal.
The process itself is simple, but trying to write it down makes it appear complicated. Let's say we want to convert 25 to binary. First write down binary place values (powers of 2) for approximately the size of number in question:
32 16 8 4 2 1_ _ _ _ _ _Note that we stopped writing down the powers of two once we got to 32, since 25 < 32. Now we need to figure out whether to put a 0 or 1 in each position. Starting from the left, write down a 1 if that place value is smaller than the number in question, in this case 25. Since 32 is not less than 25, we put down a 0 in that position. 16 is less than 25, so we put down a 1 there giving:
32 16 8 4 2 10 1 _ _ _ _We then subtract this place value from 25, giving: 25 - 16 = 9. We now continue using 9 as the number in question. Is there an "8" in 9? Yes, so we put down a 1 in that position, giving:
32 16 8 4 2 10 1 1 _ _ _9 - 8 gives 1. Both 4 and 2 are larger than 1, so we put down a 0 in both those positions, leaving us with a 1 in the last position for the final answer of:
32 16 8 4 2 10 1 1 0 0 1So 25 = %11001.
Note that the same approach is used when converting between decimal and hexadecimal, except you would use powers of 16 rather than powers of 2.
When converting between binary and hex the key is to note that each hex digit corresponds with 4 binary digits. Given the seven digits %1011110 we first add a leading zero so that the number of digits is evenly divisible by four. (You can add leading zeros without changing the value.) Then group by four starting from the right, giving us the groupings: %0101 and %1110. These respectively correspond to the hex digits $5 and $E, so %1011110 = $5E.
Again simply group each four binary digits to correspond to a single hex digit. For instance $7 = %0111, or simply %111, since leading zeros don't give any additional information. We also know that $B = %1011. This idea of grouping by four then shows us that $7B = %01111011.
Certain bit values can be interpreted to stand for characters. For instance, %01000001 = 65, which when treated as a character is 'A'. Appendix A in our text shows the correspondance between characters and code values. You can also use the man ascii unix command to show a character table.
A single bit string could mean different things, depending on how it is interpreted, i.e. the context in which it is used.
E.g. %1000001 could be:
The first electronic computers were programmed by hand by people plugging in or unplugging wires to represent 0's or 1's, which gave instructions in machine language, e.g. 01100111. This was tedious, so assembler commands were developed (e.g. ADD R3,R5), which could then be translated into machine language. Subsequently high-level languages (e.g. x = x + y) were developed, which allowed expressing mathematical ideas more directly.
The translation process of going from a higher-level language into machine language is called compiling.
If you can't solve the problem by hand, chances are you won't get the computer to do it for you either.
Consider the following example of writing a program that gives the average of students' midterm 1 grades:
- The program takes as input the grades for the 90 students. Each grade is a whole number (an integer) from 0 to 100. The results of running the program, the output, is the average (the mean) of the numbers
- What the program will need to do is to step through all the scores, accumulating the sum and keeping track of how many scores have been seen. The average will be calculated as the sum divided by the total number of scores.
- After figuring out the process described above, it is a good idea to test out this process by hand on a small problem.
- If the process seems to work correctly for a small problem, then the next step is to translate the process into a computer language such as C and type it in.
- After the program compiles and runs correctly, test it, paying careful attention to the "boundary conditions", such as when there are 0 students, or only 1 student, or more than 90 students. Also consider whether or not you need to do error checking for your input, such as verifying that each score is in fact within the 0..100 range.
More formally, the above steps of problem solving and implementation on a computer can be summarized as:
- Problem Specification: what's the input and output look like?
- Input: 90 integers ranging from 0 to 100, e.g. 95 68 100 99 ...
- Output: average of the input numbers, e.g. 90
- Algorithm Design: what are the processing steps used to transform the input into the output? E.g.
- sum = 0, counter = 1
- Read a grade
- counter = counter + 1
- sum = sum + grade
- if counter < 90 then go back to step 2
- mean = sum / 90
- display the mean
- Hand Test
- Coding
- Testing
[CS Dept] [UIC]
[Prof. Reed]