![]() |
CS1101: Laboratory 12(b)Negative Binary NumbersAcademic Year 2006-2007Lecturer: Dr. Barry O'SullivanDepartment of Computer Science University College Cork |
The objective of this lab is to ensure that students can negate a binary number. Three forms of the negative number must be computed:
In this system the leftmost bit is the sign bit (0 is + and 1 is -). The remaining bits hold the absolute magnitude of the number.
This system also has a sign bit with 0 used for plus and 1 for minus. To negate a number, replace each 1 by a 0 and each 0 by a 1. This holds for the sign bit as well.
This system also has a sign bit with 0 used for plus and 1 for minus. Negating a number is a two-step process. First, each 1 is replaced by a 0 and each 0 by a 1, just as in one's complement. Second, 1 added to the result.
This system also has a sign bit with 0 used for plus and 1 for minus. Negating a number is a two-step process. First, each 1 is replaced by a 0 and each 0 by a 1, just as in one's complement. Second, 1 added to the result. In this system, which for $m$-bit numbers is called excess 2^(m-1) represents a number by storing it as the sum of itself and 2^(m-1). For example, for 8-bit numbers, m = 8, the system is called excess 128 and a number is stored as its true value plus 128. Interestingly enough, this system is identical to two's complement with the sign bit reversed.
Binary addition is the same as decimal addition except that a carry is generated if the sum is greater than 1 rather than greater than 9. You have already comverted non-negative decimal numbers to binary in Laboratory Exercise 12(a). You may want to reuse the code you wrote in that lab.
Write a program that reads in a non-negative decimal integer and prints out the following:
An example of the output produced by the program could be as follows:
The number your entered was: 6 This number's representation in 8-bit binary is: 00000110 This number's negative representation in 8-bit signed magnitude is: 10000110 This number's negative representation in 8-bit one's complement is: 11111001 This number's negative representation in 8-bit two's complement is: 11111010 This number's negative representation in excess 128 is: 01111010
To submit your assignment please put your program in a Java file called
Negative.java
.
To submit it, type the following from the command prompt from one of the
lab machines:
cs1101submit Negative.javaThe submission deadline is as above.
Return to the CS1101 Home Page