Question

In: Computer Science

Change the Homework.java file in the ways indicated in the TODOs such that it prints all...

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();
   }

}
---

Solutions

Expert Solution

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:


Related Solutions

Write a C program which prints the environment variables to a file "sample.txt" . All variables...
Write a C program which prints the environment variables to a file "sample.txt" . All variables are followed by a newline character. example: example: inputs: envp/environ = {"E1=5","E2=9",NULL} outputs: env.txt as a string would be "E1=5\nE2=9\n".
Python 3 A simple way to encrypt a file is to change all characters following a...
Python 3 A simple way to encrypt a file is to change all characters following a certain encoding rule. In this question, you need to move all letters to next letter. e.g. 'a'->'b', 'b'->'c', ..., 'z'->'a', 'A'->'B', 'B'->'C', ..., 'Z'->'A'. For all digits, you need to also move them to the next number. e.g. '0'->'1', '1'->'2', ..., '9'->'0'. All the other symbols should not be changed. Write a function encrypt with the following requirements: the function takes a string argument,...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
Create a program that parses a CSV file of product data and prints the items with...
Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened,...
/ File: temperature.cxx // This program prints a table to convert numbers from one unit to...
/ File: temperature.cxx // This program prints a table to convert numbers from one unit to another. // The program illustrases some implementation techniques. #include // Provides cout #include // Provides setw function for setting output width #include // Provides EXIT_SUCCESS #include // Provides assert function using namespace std; // Allows all standard library items to be used double celsius_to_fahrenheit(double c) // Precondition: c is a Celsius temperature no less than absolute // zero (-273.16). // Postcondition: The return value...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Create a file and change the file permissions to (a) make it executable, (b) enable the...
Create a file and change the file permissions to (a) make it executable, (b) enable the SUID, and (c) use the ACL to add a permission for the root user. Do a long listing of this file. LINUX command line / virtual box.
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT