CompOrg Fall 2003

Number Representation and Arithmetic and C Logic

Due Date: 9/8/03 (in class)
Hardcopy only (no electronic submissions)

Problem #1 (30 points total)
 

Figure out the missing values in the tables below (all the blank cells). Pay attention to whether items are signed or unsigned, and the number of bits!.

Decimal 16 bit 2s complement binary
567 0000 0010 0011 0111
-567 1111 1101 1100 1001
3855 0000111100001111
-3856 1111000011110000

 

Decimal 16 bit unsigned binary 16 bit Hexadecimal
(4 hex digits)
65535 1111 1111 1111 1111 FFFF
4658 0001001000110010 1232
65518 1111 1111 1110 1110 FFEE

 

Decimal Floating Point IEEE Single Precision
Sign (1 bit) Exponent (8 bits) Significand (23 bits)
18.5 0 10000011 00101000000000000000000
-96.125 1 10000101 10000000100000000000000

Sample Answer

Answers are shown above in gray. 2 points each except the last one (-96.125) is worth 4 points.

 
Problem #2 (40 points)
 

Do Problem 2.43 from the textbook. Make sure that your answers are valid C expressions using only bitwise logical and logical operators!.

C bitwise logical operators:
&   |   ~   ^  
C logical operators:
&&   ||   !

Notes:

  • All we want is C expressions, not entire C programs. For example, an expression that determines whether the LS bit of x is a 1 is x & 0x01. This expression could be used as a boolean value in a C program, the expression is true only when the LS bit of x is a 1.

Sample Answer

10 points each

  1. !!x

  2. !!~x

  3. !!(x&0xFF)

  4. !!(~x&0xFF)

NOTE: There are certainly other possible correct answers - make sure they use C logical and bitwise logical operators only, not arithmetic or comparison operators!

 
Problem #3 (30 points)
 

Consider the computer addition of two integers. Both integers are representable as 8-bit 2's complement integers. The result is also an 8 bit 2's complement integer. We know that the true sum may not fit in an 8 bit 2's complement integer - your job is to write some C code that determines whether or not the answer is correct. You can't use any arithmetic that involves anything other than 8 bit 2's complement numbers, but you can use logic operations. Show some C code that will print "yes" if the computed sum is correct (the correct answer fits in an 8 bit 2's complement int) or prints "no" if the answer is wrong (the sum cannot be represented as an 8-bit 2's complement int).

Here is what the code might look like, with the sections you must write left out. NOTE: x,y and z are all signed 8 bit numbers (in C, declared as char)

/* x and y already have some values at this point */

z = x + y;   /* z now has 8 bit result. Is it right? */

if (...you need to write this...) {
   printf("yes");
} else {
   printf("no");
}

NOTE: Feel free to write it any way you want, you don't have to do the entire job with a single if statement as shown above.

HINTS: Review the conditions under which the result of 2's complement computer addition is not correct (page 70 in the text). You need to find a way to express these conditions in C, without using arithmetic (use logic operations - look at the bits of x,y and z).

Sample Answer

if (  (( x & 0x80)!=(y & 0x80)) || ( (z & 0x80) == (y & 0x80)) ) {
   printf("yes");
} else {
   printf("no");
}

Grading:

  • 10 points for correct mask (0x80 or 128)
  • 10 points for logic determining the conditions under which overflow has occurred (sign of operands is the same, and sign of the result is different).
  • 10 points for using correct operators (&, not &&, etc.).

Any solution that relies on arithmetic should lose 10 points.


NOTE: There are many correct ways of doing this (some people will notice that using XOR can be useful here). They are allowed to use multiple if statements. They are not allowed to use integer arithmetic (so they can't just compare the 8 bit result to a 32 bit result). They must use bitwise logic and/or boolean logic operators!