## [DialogJIG] Code for Lesson 2

• From: Janet_Rogers@xxxxxxxxxxxxx
• To: dialogjig@xxxxxxxxxxxxx
• Date: Fri, 2 Nov 2001 08:58:12 +1000

```Try out this code to see examples of the different types of operators at
work. As it generates a random number, it will show a different value each
time it is run.

//: c03:BitManipulation.java // Using the bitwise operators.
import java.util.*;
;
public class BitManipulation { public static void
main(String[] args) {
Random rand = new Random();
int i = rand.nextInt();
int j = rand.nextInt();
pBinInt("-1", -1);
pBinInt("+1", +1);
;
int maxpos = 2147483647;
pBinInt("maxpos", maxpos);
int maxneg = -2147483648;
pBinInt("maxneg", maxneg);
pBinInt("i", i);
pBinInt("~i", ~i);
pBinInt("-i", -i);
pBinInt("j", j);
pBinInt("i & j", i & j);
pBinInt("i | j", i | j);
pBinInt("i ^ j", i ^ j);
pBinInt("i << 5", i << 5);
pBinInt("i >> 5", i >> 5);
pBinInt("(~i) >> 5", (~i) >> 5);
pBinInt("i >>> 5", i >>> 5);
pBinInt("(~i) >>> 5", (~i) >>> 5);
;
long l = rand.nextLong();
long m = rand.nextLong();
pBinLong("-1L", -1L);
pBinLong("+1L", +1L);
long ll = 9223372036854775807L;
pBinLong("maxpos", ll);
long lln = -9223372036854775808L;
pBinLong("maxneg", lln);
pBinLong("l", l);
pBinLong("~l", ~l);
pBinLong("-l", -l);
pBinLong("m", m);
pBinLong("l & m", l & m);
pBinLong("l | m", l | m);
pBinLong("l ^ m", l ^ m);
pBinLong("l << 5", l << 5);
pBinLong("l >> 5", l >> 5);
pBinLong("(~l) >> 5", (~l) >> 5);
pBinLong("l >>> 5", l >>> 5);
pBinLong("(~l) >>> 5", (~l) >>> 5);
} static void pBinInt(String s, int i) {
System.out.println( s + ", int: " + i + ", binary: ");
System.out.print(" ");
for(int j = 31; j >=0; j--)
if(((1 << j) & i) != 0)
System.out.print("1");
else System.out.print("0");
System.out.println();
}

static void pBinLong(String s, long l) {
System.out.println( s + ", long: " + l + ", binary: ");
System.out.print(" ");
for(int i = 63; i >=0; i--)
if(((1L << i) & l) != 0)
System.out.print("1");
else System.out.print("0");
System.out.println();
} } ///:~;

And for those who missed out, here are last nights homework exercises.
Answers will be posted before the next JIG meeting.

1.      Create an application into which can be put the value of the new
computer game you are purchasing. You are using your birthday \$100 note
to pay for the game, so have the application work out the GST, total
price and change that you will get for your \$100 and print it on the
screen. (The amounts can be hardcoded in your program).

2.      Assign an integer value to an object (you work out what the
object type should be). Find out if the object is of class String.

3.      Now assign some text to an object. Find out if the object is of
class String.

4.      Compile and run the piece of code that will be posted to the
listserve tomorrow. Watch how the values of the random numbers changes
with the different uses of the operators.

5.      If you are brave enough to try to write a loop before we have
covered looping, write an application that prints out the numbers from
one to ten a) using the pre-increment operator
b) using the post-increment operator
c) using the pre-decrement operator
d) using the post-decrement operator.

.....and many thanks to those who attended last nights meeting and helped
identify all the deliberate errors in my presentation!!!!!!

Regards
Janet

=========================
Dialog JIG! Mailing List:

To subscribe/unsubscribe send an email to dialogjig-request@xxxxxxxxxxxxx with
'subscribe' or 'unsubscribe' in the Subject field.

List Archive: http://www.freelists.org/archives/dialogjig/
FAQ: email dialogjig-request@xxxxxxxxxxxxx with 'faq' in the subject field

```