07.01.2015 Views

CPSC 121: Models of Computation Assignment ... - Ugrad.cs.ubc.ca

CPSC 121: Models of Computation Assignment ... - Ugrad.cs.ubc.ca

CPSC 121: Models of Computation Assignment ... - Ugrad.cs.ubc.ca

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>CPSC</strong> <strong>121</strong>: <strong>Models</strong> <strong>of</strong> <strong>Computation</strong><br />

<strong>Assignment</strong> #2, due Thursday February 2 nd , 2011 at 17:00<br />

[14] 1. Most programming languages provide ways to manipulate the bits <strong>of</strong> signed integers<br />

represented using two’s complement. For instance, if x and y are two such integers,<br />

then<br />

• x & y denotes the integer whose i th bit is the i th bit <strong>of</strong> x and the i th bit <strong>of</strong> y.<br />

For instance, assuming that we have eight bit integers, 109 10 = 01101101 2 and<br />

51 10 = 00110011 2 , and so 109 & 51 = 00100001 2 = 33 10 .<br />

• x | y denotes the integer whose i th bit is the i th bit <strong>of</strong> x or the i th bit <strong>of</strong> y. For<br />

instance, 109 | 51 = 01111111 2 = 127 10 .<br />

• x ˆ y denotes the integer whose i th bit is the i th bit <strong>of</strong> x xor the i th bit <strong>of</strong> y.<br />

For instance, 109 ˆ 51 = 01011110 2 = 94 10 .<br />

• x ≫ i denotes the integer obtained by “deleting” the rightmost i bits <strong>of</strong> x,<br />

and adding i 0 bits to the left <strong>of</strong> the remaining bits. For instance, 109 ≫ 3 =<br />

00001101 (the bits 101 were removed).<br />

We <strong>ca</strong>n perform several operations simply by manipulating the sequence <strong>of</strong> bits that<br />

represents an integer using these operators. In this problem, we look at some <strong>of</strong><br />

them.<br />

[2] a. There are several situations where we would like to only retain some <strong>of</strong> the<br />

bits <strong>of</strong> an integer x. For instance, the last 3 bits <strong>of</strong> 109 equal 101 2 = 5 10 .<br />

Given x, what expression would give you an integer that corresponds to the<br />

last 3 bits <strong>of</strong> x<br />

[2] b. Continuing from the previous question, what expression would give an integer<br />

that corresponds to the next three bits <strong>of</strong> x (those corresponding to the<br />

positions 2 5 , 2 4 and 2 3 ) For instance, for 51, this would be 110 2 = 6 10 .<br />

[4] c. The expression x & −x gives the largest power <strong>of</strong> 2 that divides x.<br />

i. Verify that this is the <strong>ca</strong>se for the two 8-bit integers 109 and 51.<br />

ii. Explain why this is always true for every integer x. Hints: think <strong>of</strong> how<br />

you compute −x using two’s complement; this expression also helps you<br />

find the rightmost bit that is a 1.<br />

[6] d. Finding the leftmost 1 bit in a binary integer seems more compli<strong>ca</strong>ted than<br />

finding the rightmost 1 bit, and there is no simple expression like the one from<br />

part (c). However, given two unsigned integers x and y, we <strong>ca</strong>n determine<br />

whether or not their leftmost 1 bits are in the same position using the following<br />

fact:<br />

The leftmost 1 bits <strong>of</strong> x and y are in the same position if and only if<br />

x ˆ y ≤ x & y.


Explain why this statement holds. Note that, since it is a biconditional, you<br />

will need two explanations: one that explains why if the leftmost 1 bits <strong>of</strong> x<br />

and y are in the same position, then xˆy ≤ x&y, and one that explains why if<br />

the leftmost 1 bits <strong>of</strong> x and y are not in the same position, then x ˆ y > x & y.<br />

[10] 2. Computers represent characters by associating with each character a specific sequence<br />

<strong>of</strong> 0’s and 1’s. In this question, you will be dealing with the ASCII and<br />

Unicode encodings discussed in class. You <strong>ca</strong>n find a table listing the decimal (labeled<br />

Dec) and hexadecimal (labeled Hx) codes for each ASCII character (labeled<br />

Chr) at<br />

http://www.lookuptables.com<br />

by clicking on the “ASCII table” button near the top <strong>of</strong> the page. This site also<br />

contains tables for Unicode accessible by clicking on the “Unicode v4” button.<br />

[2] a. Unix and Linux systems terminate each line <strong>of</strong> a text file with a single linefeed<br />

(LF) character. MacOS terminates each line with a single <strong>ca</strong>rriage return<br />

(CR) character. Windows terminates each line with two characters: a <strong>ca</strong>rriage<br />

return character (CR) followed by a linefeed character (LF). What are the<br />

ASCII codes for these two characters<br />

[2] b. In one sentence, explain how given an integer between 0 and 9, you would<br />

obtain the ASCII code for the character representing that digit. For instance,<br />

the integer 7 would be converted to the character ’7’.<br />

[2] c. In one sentence, explain how given its ASCII code, you would convert an<br />

upper<strong>ca</strong>se letter into the corresponding lower<strong>ca</strong>se letter.<br />

[2] d. Is the operation from part (c) simpler to describe in decimal or in binary<br />

Why<br />

[2] e. What is the hexadecimal Unicode character for the picture <strong>of</strong> an airplane<br />

(hint: see “Dingbats”)<br />

[8] 3. Following on the previous question, design a circuit that has 8 inputs b 7 , b 6 , . . . b 0 ,<br />

and two outputs d and l:<br />

• d will be true if the character whose ASCII code is b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 represents<br />

a digit.<br />

• l will be true if the character whose ASCII code is b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 represents<br />

an upper<strong>ca</strong>se or lower<strong>ca</strong>se letter.<br />

Hint: do not write a truth table with 256 lines! Instead, try to find a pattern for the<br />

sequences <strong>of</strong> bits for which one or the other should be true. A brute-force working<br />

solution will be worth 6/8. To get full marks, your solution needs to be both correct<br />

and elegant (it doesn’t need to use the smallest possible number <strong>of</strong> gates, but it<br />

shouldn’t be too ugly).


[16] 4. Determine the validity <strong>of</strong> the following arguments using rules <strong>of</strong> inference. You may<br />

use inference rules from class or one <strong>of</strong> the texts (Epp or Rosen), or you may use a<br />

logi<strong>ca</strong>l equivalence from class or the texts as an inference rule. You must state what<br />

inference rule or logi<strong>ca</strong>l equivalence you use at each step.<br />

[8] a. 1. (∼p ∨ q) → r<br />

2. r → (s ∨ t)<br />

3. ∼s ∧ ∼u<br />

4. ∼u → ∼t<br />

∴ p<br />

[8] b. Given the following premises:<br />

(1) If the Toronto Maple Leafs do not win in overtime, then Donald Cerise<br />

will not be happy.<br />

(2) If the clouds do not cross Vancouver Island or an arctic wind is blowing,<br />

then it will be cold.<br />

(3) If it is cold, then the Toronto Maple Leafs will win in overtime, or the Los<br />

Angeles Kings will lose in overtime.<br />

(4) The clouds will not cross Vancouver island, or it will snow Saturday.<br />

(5) If Donald Cerise is unhappy, then the Los Angeles Kings will not lose in<br />

overtime.<br />

(6) The Toronto Maple Leafs did not win in overtime.<br />

we <strong>ca</strong>n conclude that it will snow Saturday.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!