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.
/*
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();
}
}*/
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Java BitSet
You are viewing a single comment's thread. Return to all comments →
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;