Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this Java question: Java Bitset. We are given a code to work with and please use Java 8 to answer this

Please help me with this Java question: Java Bitset. We are given a code to work with and please use Java 8 to answer this question.

image text in transcribed

image text in transcribed

image text in transcribed

Here is a code we are given to work with.

import java.io.*;

import java.util.*;

public class Solution {

public static void main(String[] args) {

/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

}

}

Java's BitSet class implements a vector of bit values (i.e.: false (0) or true (1)) that grows as needed, allowing us to easily manipulate bits while optimizing space (when compared to other collections). Any element having a bit value of 1 is called a set bit. Given 2 BitSets, B1 and B2, of size N where all bits in both BitSets are initialized to 0 , perform a series of M operations. After each operation, print the number of set bits in the respective BitSets as two space-separated integers on a new line. Input Format The first line contains 2 space-separated integers, N (the length of both BitSets B1 and B2 ) and M (the number of operations to perform), respectively. The M subsequent lines each contain an operation in one of the following forms: - AND - OR - XOR - FLIP - SET In the list above, > is the integer 1 or 2 , where 1 denotes B1 and 2 denotes B2. is an integer denoting a bit's index in the BitSet corresponding to >. For the binary operations AND,OR, and XOR, operands are read from left to right and the BitSet resulting from the operation replaces the contents of the first operand. For example: AND 21 Initially: N=5,M=4,B1={0,0,0,0,0}, and B2={0,0,0,0,0}. At each step, we print the respective number of set bits in B1 and B2 as a pair of space-separated integers on a new line. M0=AND12B1=B1B2={0,0,0,0,0}{0,0,0,0,0}={0,0,0,0,0}B1={0,0,0,0,0},B2={0,0,0,0,0} The number of set bits in B1 and B2 is 0 . M1=SET14 Set B1[4] to true (1). B1={0,0,0,0,1},B2={0,0,0,0,0}. The number of set bits in B1 is 1 and B2 is 0 . M2=FLIP22 Flip B2 [2] from false (0) to true (1). B1={0,0,0,0,1},B2={0,0,1,0,0}. The number of set bits in B1 is 1 and B2 is 1 . M3=OR21B2=B2B1={0,0,1,0,0}{0,0,0,0,1}={0,0,1,0,1}.B1={0,0,0,0,1},B2={0,0,1,0,1}. The number of set bits in B1 is 1 and B2 is 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions