Starting from:

$25

CS2110 - Homework 01 - Bitwise Operations - Solved

1           Objective
1.1         Purpose
The goal of this assignment is for you to understand bitwise operators, and how to use them to manipulate data (including integers and ASCII codes for characters) programmatically. For this assignment you will be programming in Java, because you already know this language. The concepts of bitwise operations that you learn here will be used throughout the remainder of this course, including in machine language, assembly language, and C programs.

1.2         Tasks
You will complete the Java methods in the provided files: Bases.java, Operations.java, and BitVector.java. The comments in the provided starter code describe what each method should accomplish. Please note the restrictions on which operators you are allowed to use, and which ones are prohibited for this assignment.

Please read the rest of this document for more detailed instructions and hints.


2           Instructions
1.    Make sure all 4 files are in the same folder:

(a)    Bases.java

(b)    Operations.java

(c)     BitVector.java

(d)    hw1checker.jar

2.    Ensure that you have Java 8 installed. Later versions may also work, but are untested. Then, open a terminal / command prompt and navigate to the folder that all your files are in.

3.    Alternatively, you can use the Docker container to test your code. To do this, either run ./cs2110docker.sh and open the terminal inside the graphical client, or run ./cs2110docker.sh -it to open a shell directly in your terminal. Don’t forget to put your homework files/folders in the same directory as cs2110docker.sh.

4.    Run the following command to see your grade for BitVector.java:

java -jar hw1checker.jar -g BitVector.java

5.    It should show all the test cases you are failing and give a 0/40 score.

6.    Implement one of the functions in BitVector.java and re-run step 3 until you get full credit for that part of BitVector.java.

Now complete all the other methods in each of the 3 Java files (the details on how to implement each method is described in the comment above the corresponding method). Run the verifier and autograder frequently to avoid errors and to make sure you are using only the allowed operations.

Note: We have included an Examples.java file which shows and explains examples of two methods similar to those used in your assignment, which may be useful if you get stuck or confused at any point.


1.    Make sure that the hw1checker.jar file is in the same folder as your Bases.java, BitVector.java, and Operations.java files.

2.    Navigate to this folder in your command line.

3.    Run the desired command (see below).

3.1         Commands
Test all methods and verify that no banned operations are being used (all 3 files):

java -jar hw1checker.jar

Note: Your grade will be dependent on the output of the above command, as it will both test the output of your methods, and verify that you are not using banned operations. If you get stuck though, you can use some of the below commands to help you debug.

On Windows and Mac, you can also double click the hw1checker.jar in your file explorer to test and verify all 3 files. The results will be placed in a new file called gradeLog.txt. Any errors with compilation, infinite loops, or other runtime errors will be placed in a new file called errorLog.txt.

Test & verify all methods in a single file (using Bases.java). Useful for when you just want to look at one file at a time. For example:

java -jar hw1checker.jar -g Bases.java

Test all methods in a single file without running verifier (using Bases.java). This means that this will only run the unit tests, and will not check for the use of banned operations. Useful for when you just want to try and get something that works. For example:

java -jar hw1checker.jar -t Bases.java

Verify all methods in a single file without running tests (using Bases.java for example):

java -jar hw1checker.jar -v Bases.java

Any combination of files can also be graded, tested, or verified at the same time. For instance, Bases and Operations can be graded simultaneously as follows:

java -jar hw1checker.jar -g Bases.java Operations.java


6           Hints
6.1         Printing numbers in different bases
Remember that all numbers are stored in your computer as binary. When you perform operations such as System.out.println(), the computer does the translation into another base for you. All you need to do is tell the computer how you are representing your numbers, and how to interpret them.

For example, you can specify 16 in decimal, octal, or hexadecimal like so:

System.out.println(16);                            // decimal (base 10), the default

System.out.println(020); // octal (base 8), precede the number with a zero

System.out.println(0x10); // hexadecimal (base 16), precede the number with a "0x" (zero x)

You can also tell Java to print out your number in different bases using a method called printf. printf is the GRANDDADDY of all printing functions! When we get to C programming, you will be using it a lot. It is useful if you would like to write your own tester as well.

printf takes a variable number of arguments, the first of which is a format string. After the format string come the parameters. The formatting for the numbers is controlled by the format string.

6.1.1         Example
System.out.printf("In decimal: %d", 16); System.out.printf("In octal: %o", 16);

System.out.printf("In hexadecimal: %x", 16);

The %d, %o, or %x get replaced by the parameter passed in. printf does not support printing the number out in binary.

For more information about printf read http://en.wikipedia.org/wiki/Printf.

6.2         Multiplication and division
You may find that there are times in which you need to use division or multiplication, but are not allowed to. Recall from lecture that you can use bitshifting to multiply or divide by powers of 2; this concept isn’t found in the book, but is in the lecture slides.

More products