In: Computer Science
Change the Homework.java file in the ways indicated in the TODOs such that it prints all true values. The way I've given you the code it prints false for everything. You're going to use &, |, >> and << to change numbers.
Submit:
1. Your completed Homework.java
----
public class Homework {
/**
* TODO: change bitmask1 and bitmask2 so that this
prints true twice.
*/
public static void usingAND() {
System.out.println("Testing &
...");
int bitmask1 = 0x00000000;
int value1 = 0xFFFFFFFF;
System.out.println((bitmask1 &
value1) == 3);
int bitmask2 = 0x00000000;
int value2 = 0x00FFFFFF;
System.out.println((bitmask2 &
value2) == 256);
}
/**
* TODO: change bitmask1 and bitmask2 so that this
prints true twice.
*/
public static void usingOR() {
System.out.println("\nTesting |
...");
int bitmask1 = 0x00000000;
int value1 = 0x00000001;
System.out.println((bitmask1 |
value1) == 3);
int bitmask2 = 0x00000000;
int value2 = 0x00000100;
System.out.println((bitmask2 |
value2) == 257);
}
/**
* TODO
* Change value1 into 256
* Change value2 into 768
*/
public static void usingLeftShift() {
System.out.println("\nTesting
<<...");
int value1 = 0x00000001;
int shiftThisFar1 = 0; // how far
should we shift the binary 1?
System.out.println((value1 <<
shiftThisFar1) == 256);
int value2 = 3; // aka 00000000
00000000 00000000 00000011
int shiftThisFar2 = 0; //how far
should we shift these bits?
System.out.println((value2 <<
shiftThisFar2) == 768);
}
/**
* TODO
* Change value1 into 1
* Change value2 into 1
*/
public static void usingRightShift() {
System.out.println("\nTesting
<<...");
int value1 = 256; // I'm writing it
in decimal rather than hex for no special reason.
// Note that 256 in binary is
"00000000 00000000 00000001 00000000"
int shiftThisFar1 = 0; // how far
should we shift the binary 1?
System.out.println((value1 >>
shiftThisFar1) == 1);
int value2 = 0xFF; // 00000000
00000000 00000000 11111111 how to make this number into 1?
int shiftThisFar2 = 0;
System.out.println((value2 >>
shiftThisFar2) == 1);
}
public static void main(String[] args) {
usingAND();
usingOR();
usingLeftShift();
usingRightShift();
}
}
---
Note:I have renamed the file from Homework.java to Main.java because i used classname as Main.you can rename the class name as Homework and save the file as Homework.java and you can execute the below code
The changes in code are included in bold colour:
public class Main {
/**
* TODO: change bitmask1 and bitmask2 so that this prints true
twice.
*/
public static void usingAND() {
System.out.println("Testing & ...");
int bitmask1 = 0x00000003; //changed from 0x00000000 to
0x00000003
int value1 = 0xFFFFFFFF;
System.out.println((bitmask1 & value1) == 3);
int bitmask2 = 0x00000100; //changed from 0x00000000 to
0x00000100
int value2 = 0x00FFFFFF;
System.out.println((bitmask2 & value2) == 256);
}
/**
* TODO: change bitmask1 and bitmask2 so that this prints true
twice.
*/
public static void usingOR() {
System.out.println("\nTesting | ...");
int bitmask1 = 0x00000003; //changed from 0x00000000 to
0x00000003
int value1 = 0x00000001;
System.out.println((bitmask1 | value1) == 3);
int bitmask2 = 0x00000101; //changed from 0x00000000 to
0x00000101
int value2 = 0x00000100;
System.out.println((bitmask2 | value2) == 257);
}
/**
* TODO
* Change value1 into 256
* Change value2 into 768
*/
public static void usingLeftShift() {
System.out.println("\nTesting <<...");
int value1 = 0x00000001;
int shiftThisFar1 = 8; // how far should we shift
the binary 1?
//changed shiftThisFar1 from 0 to 8
System.out.println((value1 << shiftThisFar1) == 256);
int value2 =3; // aka 00000000 00000000 00000000 00000011
int shiftThisFar2 = 8; //how far should we shift
these bits?
//changed shiftThisFar2 from 0 to 8
System.out.println((value2 << shiftThisFar2) == 768);
}
/**
* TODO
* Change value1 into 1
* Change value2 into 1
*/
public static void usingRightShift() {
System.out.println("\nTesting <<...");
int value1 = 256; // I'm writing it in decimal rather than hex for
no special reason.
// Note that 256 in binary is "00000000 00000000 00000001
00000000"
int shiftThisFar1 = 8; // how far should we shift
the binary 1?
//changed shiftThisFar1 from 0 to 8
System.out.println((value1 >> shiftThisFar1) == 1);
int value2 = 0xFF; // 00000000 00000000 00000000 11111111 how to
make this number into 1?
int shiftThisFar2 = 7;//changed shiftThisFar2 from 0 to
7
System.out.println((value2 >> shiftThisFar2) == 1);
}
public static void main(String[] args) {
usingAND();
usingOR();
usingLeftShift();
usingRightShift();
}
}
Screenshot code:
Output: