Question: Bit-Array Class Create a class that stores a 64-bit long value. The class should have a method to flip the bits at each position in

Bit-Array Class

Create a class that stores a 64-bit long value. The class should have a method to flip the bits at each position in the number (up to 64 positions), as well as a method that inspects the state of the bit at each position. When interacting with the class a user should only see boolean values when assigning or retrieving values at a position in the binary value as seen in the class diagram below:

BitArray
- data : long
+ get(index : int) : boolean + set(index : int, value : boolean) : void

Field & Method Hints:

* data: This field should be a long value. There should not be any other fields in the class.

* get(index): This method returns a true value if the bit at the given index is 1, otherwise if the bit is 0 then the method returns false.

* set(index, value): Assigns the bit at the given index 1 if value is true, otherwise if value is false the bit is assigned 0.

Test your class thoroughly. In particular make sure you can assign both true and false to an index. Also, make sure you can assign values to the bits at index 0 and 63 (the MSB).

 BitArray array = new BitArray(); //turn on a few bits array.set(16, true); array.set(8, true); array.set(42, true); //verify the bits we turned on as well as bits we never turned on System.out.println("get(16)? " + array.get(16)); //true System.out.println("get(32)? " + array.get(32)); //false System.out.println("get(42)? " + array.get(42)); //true //turn off a few bits array.set(16, false); array.set(42, false); //verify the bits we turned off System.out.println("get(16)? " + array.get(16)); //false System.out.println("get(42)? " + array.get(42)); //false

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!