Espresso: A Concentrated Introduction to Java

# Laboratory: Binary Representation of Integers

Summary: In this laboratory, you will explore the binary representation of integers in Java.

Class Documentation::

Contents

## Exercises

### Exercise 0: Preparation

b. Create a new project for this lab, called `Binary`, and package within that project, called `username.binary`. Note that you should use your own username in place of username.

```mkdir /home/username/CSC152/Binary
```

c. Start Eclipse and load this project.

### Exercise 1: Confirming Prior Results

In the lab on numbers, you discovered that when you added a small value to the largest `int` (`Integer.MAX_VALUE`), you got a negative number and when you subtracted a small value from the smallest `int` (`Integer.MIN_VALUE`), you got a positive number.

The reading on binary representation suggests why: Java limits the number of bits with which it represents numbers and uses two's complement notation.

Redo the experiment, printing out the binary representation of each value. That is, print the binary representation of `Integer.MAX_VALUE`, the small integer you added, the sum of those two values, `Integer.MIN_VALUE`, the small integer you subtracted, and the difference of those two values.

Recall that you can use `Integer.toBinaryString(i)` to extract the binary representation of an `int`.

### Exercise 2: From Decimal to Binary

Write a class, `DecimalToBinary`, that prompts the user for two integers in base-ten form, prints their representation in binary, prints their sum in binary, and prints their difference in binary.

For example,

```Enter an integer: 10
Enter another integer: -3
1010 + 11111111111111111111111111111101 = 111
1010 - 11111111111111111111111111111101 = 1101
```

Note that you will need to use `Integer.parseInt` to convert the input strings to `int` values.

One disadvantage of the prior output is that it doesn't make it immediately clear how the two added values line up. Write a new version of the class that does vertical addition, giving output something like the following:

```Enter an integer: 10
Enter another integer: -3

1010
+ 11111111111111111111111111111101
----------------------------------
111

SUBTRACTION
1010
- 11111111111111111111111111111101
----------------------------------
1101
```

Note that you can add the leading spaces by a clever combination of `append`, `substring`, and `Integer.numberOfLeadingZeros(i)`.

### Exercise 4: Exploring Bitwise Operations

Write a program, `BitOps`, that allows you to explore the various bitwise operations (`&`, `|`, `^`, `>>`, `<<`).

For example, here's part of the output.

```Enter an integer: 10
Enter another integer: 2

BITWISE AND
00000000000000000000000000001010 (10)
& 00000000000000000000000000000011 (3)
----------------------------------
00000000000000000000000000000010 (2)

LEFT SHIFT
10 << 3 = 80
00000000000000000000000000001010 << 3
=> 00000000000000000000000001010000
```

## History

Tuesday, 7 February 2006 [Samuel A. Rebelsky]

• Created.

This page was generated by Siteweaver on Thu Mar 30 15:24:34 2006.
The source to the page was last modified on Tue Feb 7 09:13:03 2006.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/binary.html`.

You may wish to validate this page's HTML ; ; Check with Bobby

Samuel A. Rebelsky
rebelsky@grinnell.edu