CS 2731 Computer Organization II -- Spring 2003
Laboratory 2 [Jan 22]: Binary Arithmetic


The basic laboratory:
Laboratory 2 is an introduction to binary arithmetic and representations of signed numbers.

For this laboratory, you are to experiment with the bit pattern used to represent an int 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.)

Given either an "ordinary" integer (in decimal) or a bit pattern, it is easy to get the other representation, say, with a simple program like the following in C. (A similar program in Java is here.)

#include <stdio.h>
#include <ctype.h>
void main(void){
   int i;
   char ch;
   for( ; ; ) {
      while (isspace(ch = getchar()))
         ;
      if (ch == 'x') {
         scanf("%x", &i);
         printf("  Decimal: %10i\n", i);
         printf("  Bits: %08x \n", i);
      }
      else if (ch == 'i') {
         scanf("%i", &i);
         printf("  Decimal: %10i\n", i);
         printf("  Bits: %08x \n", i);
      }
      else break;
   }
}
Typical output looks like the following (user input in boldface):
i 47
  Decimal:         47
  Bits: 0000002f 
i -3
  Decimal:         -3
  Bits: fffffffd 
x ffffffff
  Decimal:         -1
  Bits: ffffffff 
q
For this laboratory, experiment with the program by entering various integers, including 0, 1, 2, -2, -3, 2147483647, 2147483648 (overflow), -2147483648, and -2147483649 (underflow).


Sample 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:
    a) An unsigned binary number
    b) A 16-bit two's complement number

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

  3. Convert binary number 10011111001 to:
    a) octal
    b) hexadecimal
    c) decimal

  4. Perform the following binary addition:
    10000111 + 10111011

  5. Convert the hexadecimal number AB32 to:
    a) binary
    b) octal
    c) decimal


The answers are:
  1. a) 10111101
    b) 0000000010111101
  2. b) 1111111101000011
  3. a) 2371
    b) 4F9
    c) 1273
  4. 101000010
  5. a) 1010 1011 0011 0010
    b) 125462
    c) 43,826


Extra for those bored with the above:
Here are more interesting programs that convert between number bases without using the C printf formatting.

Base conversions in C: here.

Base conversions in Java: here.


Revision date: 2003-01-09. (Please use ISO 8601, the International Standard.)