Computer Science 15-111 (Sections A & B), Spring 2007

Homework #2 (part 1)

  Due:  Mon 22-Jan-2007 as you enter class.

 

The following problems are to be solved without use of a calculator or a computer.  Work them by hand.  (This is what you will need to do on upcoming quizzes and tests!).

 

What to submit:  A hardcopy of your answers, labeled “15-111 hw 2 part 1”, with your name, section, and CA’s name all clearly denoted at the top of the page.

 

1.      Express -122 in 8-bit 2’s complement (that is, as a signed byte).

2.      For what value of x does ((byte) 137) == x) evaluate to true?

3.      Convert 0xE3 to binary, octal, and decimal.

4.      Convert decimal 173 to binary, octal, and hexadecimal.

5.      We know that (1 + 2 + 4 + 8 + ... + 2n) + 1 = 2(n+1).  Rewrite this statement in binary (rather than decimal) and briefly explain why writing it this way makes it plainly true.

6.      In just a few words of plain English, assuming n and m are positive int’s, describe under what conditions x will be set to true in this code:
   boolean x = (n / m * m == n);

7.      For what two values of byte b does (((byte)-b) == b)) evaluate to true?

8.      In Two’s Complement, the number 111…1 always represents what decimal number?

9.      The following code will not loop forever.  Very briefly, why not?

int x = 1;
while (x > 0) x++;

 

10.           What will the following code print out?
int a = 68, b = 3;
int x = a++ - --b;
System.out.printf("%c%c%3d",x,a,b);


11.           What will the following code print out?
System.out.println(1 + '2' % 3 * 4);