CSCI 210: Lab 2

Bitwise Operations
Due: 10:00 PM on Monday, March 4th

Write a Java program that performs bit operations on integers.

Preliminaries

Click on the assignment link

Once you have accepted the assignment, you can clone the repository on your computer by following the instructions and begin working.

Program Specification

In the repository you will find a file named BitDriver.java with some testing code. You will create a new file named BitOps.java that countains your code, and make sure that your code compiles and runs correctly with this test code.

In this assignment, you will write a java class named BitOps with seven different functions, described below. In this assignment, you will only use the java bit operators & (and), | (or),^ (xor), >> (right shift), >>> (right shift logical), and << (left shift). You will not receive credit for the assignment if you use addition, subtraction, division, multiplication, or mod/remainder. Additonally, you are not allowed to use if statements or for loops.

You will perform bit operations directly on the integer passed in on the command line, and you are not allowed to convert or change that integer in any way. In particular, you may not convert that integer into a string. For any function that asks you to change specific bits, you should assume bits are numbered 31 to 0, with 31 being the Most Significant Bit or highest bit, and 0 being the Least Signifcant Bit or lowest Bit. Most of these functions can be written in a single line of code.

For example, say I asked you to write a function named oneIsOne that sets bit one of an integer to one while leaving the rest of the bits unchanged. My code for that function would look like this:

    
      public static int oneIsOne(int x){
          return x | 2;
      }
    
  

It may be helpful to know that you can specify the value of an int in hex by prepending it with 0x, e.g. int x = 0xBADBEEF. You can also specify the value of an int in binary by prepending it with 0b, e.g. int x = 0b0110.

You will need to write the following functions:

public static int isOdd(int x). This function returns 1 if the number is odd, and 0 if it is even. It returns 0 on 6, and 1 on 5.

public static int divBy4(int x). This functions performs integer division by 4. It returns 1 on 6, 3 on 13, 0 on 3. Don't worry about negative numbers.

public static int nearestOdd(int x). This function rounds up the number to the nearest odd number. It returns 7 on 6, 5 on 5, and -3 on -4.

public static int flipParity(int x). This function adds 1 to even numbers, and subtracts one from odd numbers. It returns 7 on 6, 4 on 5, and -4 on -3.

public static int isNegative(int x). This function returns 0 if x is positive, and 1 if x is negative. It returns 0 on 4, and 1 on -3.

public static long findRoutingAddress(long x). IPv4 Network addresses are stored as 32 bit integers. Each address is divided into two parts: the routing address, which addresses the network the computer is on (for example, Oberlin's network), and the host address, which identifies as specific computer on that network. A subnet mask is used to zero out the host address, leaving just the routing prefix. Your task in this function is to writing a function that returns the routing addresses for a network with 256 host addresses: these routing addresses will consist of the 24 highest order bits of the address, with the last 8 bits of the address set to 0.

Note that this function takes in a long and outputs a long: this is because Java does not have unsigned ints, so if we hold a valid IP address in an int, it treats it as a negative number. Since the 32 high order bits of this long will always be zero, you can ignore them. You may note that when you put in an int that is a valid IP address, it shows up as a negative number input to all your other functions - this is because Java is treating it as a signed number. This website will convert IP addresses to integers for you, which may be useful for testing. Note that the BitDriver code prints out your results in both classic IP form (x.x.x.x) and as an integer. If you code this function correctly, the IP form of your result should look like x.x.x.0

public static int setRead(int x). The chmod, or change mode, command in Unix is used to change access permissions. It accepts user permission flags as a 3 digit octal number, where the first digit corresponds to the User permissions, the second digit corresponds to the Group permissions, and the third digit corresponds to the Others permissions. Each octal digits corresponds to three bits, so we are only using the last 9 bits of the integer. For each digit, we set the most signficant bit of the digit to 1 to give read permission, the middle bit to 1 to give write permission, and the least significant bit to 1 to give execute permission.

For this function, take in a number that represents a set of permissions, and set all the read permission bits to 1. All other permission bits should remain unchanged. Good values for testing are: 0, which should change to 444 in octal, or 292 in decimal; 64 (100 in octal, i.e. execute for user and no permissions for anyone else), which should change to 544 in octal, or 356 in decimal, and 146 (222 in octal, or only write permissions set for everyone), which should change to 666 in octal or 438 in decimal.

Submission

Make sure the final version of your code is pushed to your github repository.
C. Taylor, S. Checkoway