CSC231 Homework 5 2012
--D. Thiebaut 09:40, 3 October 2012 (EDT)
Contents
Problem #1
Assume that we want to print the first 60 terms of the series of Homework 4 using a Java program. We are aware of limitations due to the range of integers, so we use the long data type to represent the terms of the series.
Here's our first shot at this program (sorry, undocumented... just a first rough version ;-):
// Hw5a.java
// Prints first terms of recursive series:
// F[1] = 1, F[2] = 1, F[n] = 2 * F[n-1] + F[n-2] - 1
// Stops when negative value comes up...
// To compile and run:
// javac Hw5a.java
// ./Hw5a
//
class Hw5a {
public static void main(String[] args ) {
long Fn, Fn_1, Fn_2;
int count;
Fn_1 = 1;
Fn_2 = 1;
System.out.println( "F[1] = 1" );
System.out.println( "F[2] = 1" );
count = 2;
while ( true ) {
Fn = 2 * Fn_1 + Fn_2 - 1;
Fn_2 = Fn_1;
Fn_1 = Fn;
System.out.println( "F[" + count + "] = " + Fn );
count++;
if ( Fn <= 0 )
break;
}
}
}
You can create this program and run it. You will observe that its last output is:
F[53] = -6917272433323338267
Your Assignment
- Find a way to generate the exact, unsigned values in decimal of the last 7 terms of the series of 60 terms. In other words find a way to generate the exact unsigned values of F[53] to F[60].
- Your answer should give these values, and explain how you obtained them.
Grading
- Extra credits if your answer is a program written in a non interpreted language...
- Lower credits (up to B only) for solutions done by hand... (After all, we are programmers, and we don't want to do work a computer can do for us!)
Problem #2
What is the decimal equivalent of the following 16-bit 2's complement integers shown here in hexadecimal? In other words, these is sometimes referred to as short ints in some languages.
- 0xFFFE
- 0x1000
- 0x00FF
- 0x7FFF
- 0x000F
- 0x1111
Problem #3
Something interesting about all negative numbers, that is best illustrated with -1. In 2's complement, the binary representation of -1 is 11111...111. It does not matter how many bits are used in the integer format you pick, -1 is all 1s. For example, in 8-bit bytes, -1 is 11111111. In 16-bit words, -1 is 1111 1111 1111 1111. In 32-bit words, -1 is 1111 1111 1111 1111 1111 1111 1111 1111. Explain why.
Problem #4
What are the binary and hexadecimal representations of these 2's complement numbers? Assume they are defined as 16-bit words: Please refrain from using a calculator! (The list of powers of 2 shown below might be useful for this problem.)
- 128
- -128
- 1024
- -1024
- 32767
- -32767
- -2
Powers of 2
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1,024
2^11 = 2,048
2^12 = 4,096
2^13 = 8,192
2^14 = 16,384
2^15 = 32,768
2^16 = 65,536
2^17 = 131,072
2^18 = 262,144
2^19 = 524,288
2^20 = 1,048,576
2^21 = 2,097,152
2^22 = 4,194,304
2^23 = 8,388,608
2^24 = 16,777,216
2^25 = 33,554,432
2^26 = 67,108,864
2^27 = 134,217,728
2^28 = 268,435,456
2^29 = 536,870,912
2^30 = 1,073,741,824
2^31 = 2,147,483,648
2^32 = 4,294,967,296
2^33 = 8,589,934,592
2^34 = 17,179,869,184
2^35 = 34,359,738,368
2^36 = 68,719,476,736
2^37 = 137,438,953,472
2^38 = 274,877,906,944
2^39 = 549,755,813,888
2^40 = 1,099,511,627,776
2^41 = 2,199,023,255,552
2^42 = 4,398,046,511,104
2^43 = 8,796,093,022,208
2^44 = 17,592,186,044,416
2^45 = 35,184,372,088,832
2^46 = 70,368,744,177,664
2^47 = 140,737,488,355,328
2^48 = 281,474,976,710,656
2^49 = 562,949,953,421,312
2^50 = 1,125,899,906,842,624
2^51 = 2,251,799,813,685,248
2^52 = 4,503,599,627,370,496
2^53 = 9,007,199,254,740,992
2^54 = 18,014,398,509,481,984
2^55 = 36,028,797,018,963,968
2^56 = 72,057,594,037,927,936
2^57 = 144,115,188,075,855,872
2^58 = 288,230,376,151,711,744
2^59 = 576,460,752,303,423,488
2^60 = 1,152,921,504,606,846,976
2^61 = 2,305,843,009,213,693,952
2^62 = 4,611,686,018,427,387,904
2^63 = 9,223,372,036,854,775,808
2^64 = 18,446,744,073,709,551,616
Submission
- Write your answers for all the problems above in one single text file called hw5.txt and submit it as follows:
rsubmit hw5 hw5.txt
- If you wrote a program for Problem #1, include the listing of your program in the text file as well.