CS1101: Laboratory 12(b)

Negative Binary Numbers

Academic Year 2006-2007

Lecturer: Dr. Barry O'Sullivan
Department of Computer Science
University College Cork

b.osullivan@cs.ucc.ie


Objective of this Lab

The objective of this lab is to ensure that students can negate a binary number. Three forms of the negative number must be computed:

  1. Signed Magnitude
  2. One's Compliment
  3. Two's Compliment
  4. Excess Notation
Here are some notes on these systems.

Signed Magnitude

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.

One's Compliment

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.

Two's Compliment

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.

Excess Notation

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.

Note

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.


Tasks

Write a program that reads in a non-negative decimal integer and prints out the following:

  1. The decimal number in 8-bit binary
  2. The negative of the decimal number in 8-bit Signed Magnitude
  3. The negative of the decimal number in 8-bit One's Compliment
  4. The negative of the decimal number in 8-bit Two's Compliment
This lab should be submitted before Friday 23rd of March.

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


Submission

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.java
The submission deadline is as above.


b.osullivan@cs.ucc.ie

Return to the CS1101 Home Page