Computer
Organization and Design
(Textbook. Click for information.)
 CS 2733/2731
 Computer Organization II
 Fall 2004

 Recitation 2
 Binary Arithmetic
    Week 2: Aug 30-Sep 3
 Due (on time): 2004-09-08  23:59:59
 Due (late):        2004-09-12  23:59:59

Recitation 2 must be submitted following directions at: submissions on or before
  • 2004-09-08  23:59:59 (that's Wednesday, 8 September 2004, 11:59:59 pm) for full credit.
  • 2004-09-12  23:59:59 (that's Sunday, 12 September 2004, 11:59:59 pm) for 75% credit.

Outline: Recitation 2 is an introduction to binary arithmetic and representations of signed binary integers.

For this recitation, you are to experiment with the bit patterns used to represent ints in computers that use 2's complement to represent 32-bit integers. (See text, Section 4.2, pages 210-220. In C/C++ on Sun hardware the int type is a 32-bit 2's complement signed integer. Java uses the same representation on any hardare.)

Given either an "ordinary" integer (in decimal) or a bit pattern, it is easy to get the other representation, say, with programs like the following in C and in Java. The Java program uses another class to read the integer: GetData.java. Both the C and the Java use the Unix command line, rather than a development environment.

C Program Java Program

/* integer_rep.c: Dec and Hex */
#include <stdio.h>
#include <ctype.h>
int main() {
   int i;
   char ch;
   for( ; ; ) {
      while (isspace(ch = getchar()))
         ;
      if (ch == 'x') {
         scanf("%x", &i);
         printf("  Dec:    %10i\n", i);
         printf("  Hex:      %08x \n", i);   
      }
      else if (ch == 'i') {
         scanf("%i", &i);
         printf("  Dec:    %10i\n", i);
         printf("  Hex:      %08x \n", i);
      }
      else break;
   }
}

// IntegerRep.java: Dec, Bin, and Hex
public class IntegerRep {

   // main function to try out Base class
   public static void main (String[] args) {
      GetData data = new GetData();
      while (true) {
         System.out.print(" Enter int ---> ");
         int n = data.getNextInt();
         if (n == 0) break;
         String s = Integer.toBinaryString(n);
         /* to put in leading zeros */
         while (s.length() < 32) s = "0" + s;
         String t = Integer.toHexString(n);
         while (t.length() < 8) t = "0" + t;
         System.out.println("  Dec: " + n);
         System.out.println("  Bin: " + s);
         System.out.println("  Hex: " + t);
      }
   } // end of main
}
C Run and Output Java Run and Output
% cc -o integer_rep integer_rep.c
% integer_rep
 i 47
  Dec:            47
  Hex:      0000002f
 i -3
  Dec:            -3
  Hex:      fffffffd
 q
%
% javac GetData.java
% javac IntegerRep.java
% java IntegerRep
 Enter int ---> 47
  Dec: 47
  Bin: 00000000000000000000000000101111
  Hex: 0000002f
 Enter int ---> -3
  Dec: -3
  Bin: 11111111111111111111111111111101
  Hex: fffffffd
 Enter int ---> 0
%

As part of the recitation work, you must copy and execute both of these programs, recording the runs and results for the following input integers:

  1.   1  
  2.  -1  
  3.  -2  
  4.   16  
  5.  -16  
  6.  -17  
  7.   4096  
  8.   2147483647   (largest legal int)
  9.   2147483648   (overflow)
  10.  -2147483648   (smallest legal int)
  11.  -2147483649   (underflow)

If you haven't had the advanced programming course and haven't done any C, run the C program anyway, using the commands in the table above. For the Java program, you can use the Unix-style commands or you may use the Borland JBuilder that many of you are accustomed to.


Sample Quiz and Exam Questions:
Here are some sample problems of the type you should expect on the first quiz and the first exam.

  1. Convert 189 to:
    i) An unsigned binary number
    ii) A 16-bit two's complement number

  2. Convert -189 to:
    i) A 16-bit two's complement number

  3. Convert binary number 10011111001 to:
    i) octal
    ii) hexadecimal
    iii) decimal

  4. Perform the following binary addition:
    10000111 + 10111011

  5. Convert the hexadecimal number AB32 to:
    i) binary
    ii) octal
    iii) decimal

  6. Consider 4-bit signed 2's complement integers. Write out all possible integer values as bit strings of length 4, ordering them from smallest to largest. In each case give the corresponding decimal value.


Number Base Conversions:

Here are more interesting programs that convert between number bases without using the C printf formatting or special Java library functions.

Base conversions: in C,    in Java.


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate number 1-3.

  Contents of submission for Recitation 2:

Last Name, First Name; Course Number; Recitation Number (2).

  1. A log of runs of the two programs (C and Java) with each of the 11 inputs, for 22 inputs altogether.

  2. Answers to the Sample Quiz and Exam Questions.

  3. Download and run one or the other of the C or Java base conversion programs (not both). Show a log of your run with resulting output. In particular, for the C program, show what the number 12121212 base 3 is equal in bases 2, 8, and 10. For the Java program, convert 4100 (base 10) to bases 2, 3, and 8.


Revision date: 2004-06-22. (Please use ISO 8601, the International Standard.)