We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
cause switch will implements all cases under what it called, for example switch calls "FLIP" case, it will implement it, and then it will implement the "SET" case without break
i have one doubt ,we are considering n as number of bits in bitstings B1 and B2 ,in this code we are just reading n but not using n anywhere to set n as size of B1 and B2,how code takes n as a size??
/*
import java.util.BitSet; //this code compares two bitset arrays of 1's and 0's
import java.util.Scanner;
public class javaBitsetwork {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt(); //asks for size of each bitset array
int m = scan.nextInt(); //asks for number of operations you're gonna give it for the bitsets
BitSet b1 = new BitSet(n); //first bitset array
BitSet b2 = new BitSet(n); //second bitset array
BitSet[] bitset = new BitSet[3]; //larger bitset array that the first two are going into (making an "x,y" 2D array)
bitset[1] = b1; //puts first bitset array in larger bitset array
bitset[2] = b2;
while ( 0 < m-- ) { //performs the operations
String op = scan.next();
int x = scan.nextInt();
int y = scan.nextInt();
switch (op) {
case "AND": //everything is 0 so everything is 0
bitset[x].and(bitset[y]); //https://www.youtube.com/watch?v=4hhWVy8VtxY
break;
case "SET":
bitset[x].set(y); //this sets x index of the big array (so small array 1), with y index of
//the small array to true (any value above 0 makes it true)
break;
case "FLIP": //flips a bit from false to true and vice versa I assume
bitset[x].flip(y);
break;
case "OR": //if one is true then they're both true
bitset[x].or(bitset[y]);
break;
case "XOR": //if one is false then they're both false
bitset[x].xor(bitset[y]);
}
System.out.println("this is x:" + x + " b1: " + b1+ " b2: " + b2);
System.out.printf("%d %d%n", b1.cardinality(), b2.cardinality()); //prints out the "cardinality" aka the number of elements..
//in each set
}
scan.close();
}
}*/
/*
import java.util.BitSet; //this code compares two bitset arrays of 1's and 0's
import java.util.Scanner;
public class javaBitsetwork {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt(); //asks for size of each bitset array
int m = scan.nextInt(); //asks for number of operations you're gonna give it for the bitsets
BitSet b1 = new BitSet(n); //first bitset array
BitSet b2 = new BitSet(n); //second bitset array
BitSet[] bitset = new BitSet[3]; //larger bitset array that the first two are going into (making an "x,y" 2D array)
bitset[1] = b1; //puts first bitset array in larger bitset array
bitset[2] = b2;
while ( 0 < m-- ) { //performs the operations
String op = scan.next();
int x = scan.nextInt();
int y = scan.nextInt();
switch (op) {
case "AND": //everything is 0 so everything is 0
bitset[x].and(bitset[y]); //https://www.youtube.com/watch?v=4hhWVy8VtxY
break;
case "SET":
bitset[x].set(y); //this sets x index of the big array (so small array 1), with y index of
//the small array to true (any value above 0 makes it true)
break;
case "FLIP": //flips a bit from false to true and vice versa I assume
bitset[x].flip(y);
break;
case "OR": //if one is true then they're both true
bitset[x].or(bitset[y]);
break;
case "XOR": //if one is false then they're both false
bitset[x].xor(bitset[y]);
}
System.out.println("this is x:" + x + " b1: " + b1+ " b2: " + b2);
System.out.printf("%d %d%n", b1.cardinality(), b2.cardinality()); //prints out the "cardinality" aka the number of elements..
//in each set
}
scan.close();
}
}*/
Java BitSet
You are viewing a single comment's thread. Return to all comments →
Lots of clever solutions here. Here's my refactored version after stealing some gems from these discussions. Thanks guys! ;-)
bitset[0] wasted!!😉
Why is the break clause necessary?
cause switch will implements all cases under what it called, for example switch calls "FLIP" case, it will implement it, and then it will implement the "SET" case without break
You can also used a if - else ladder here then why you use a switch case here???? is there any specific reason for this???????
Clean code
simple and readable
i have one doubt ,we are considering n as number of bits in bitstings B1 and B2 ,in this code we are just reading n but not using n anywhere to set n as size of B1 and B2,how code takes n as a size??
here the same code but with comments and a YT link, you're welcome future coders
/* import java.util.BitSet; //this code compares two bitset arrays of 1's and 0's import java.util.Scanner;
/* import java.util.BitSet; //this code compares two bitset arrays of 1's and 0's import java.util.Scanner;
quite cheeky way of using BitSet array :)