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.
Scannersc=newScanner(System.in);intn=sc.nextInt();//length of bits in the numberintm=sc.nextInt();//total operations BitSetb1=newBitSet(n);//b1 contains the bits with n lengthBitSetb2=newBitSet(n);//b2 contains the bits with n lengthfor(inti=0;i<m;i++){Stringip=sc.next();//Reading the string from the user AND, OR, XOR, SET, FLIPswitch(ip.charAt(0)){case'A':if(sc.nextInt()==1){//If input is 1 then and operation between b1 and b2//Otherwise b2 and b1.b1.and(b2);}else{b2.and(b1);}break;case'O':if(sc.nextInt()==1){//If input is 1 then and operation between b1 or b2//Otherwise b2 or b1.b1.or(b2);}else{b2.or(b1);}break;case'X':if(sc.nextInt()==1){//If input is 1 then and operation between b1 xor b2//Otherwise b2 xor b1.b1.xor(b2);}else{b2.xor(b1);}break;case'F':if(sc.nextInt()==1){//If input is 1 then flip the value in the //index value given by user, in the b1.//Otherwise filp the index value in the b2.b1.flip(sc.nextInt());}else{b2.flip(sc.nextInt());}break;case'S':if(sc.nextInt()==1){//If input is 1 then set the value in the //index value given by user, in the b1.//Otherwise set the index value in the b2.b1.set(sc.nextInt());}else{b2.set(sc.nextInt());}break;}System.out.println(b1.cardinality()+" "+b2.cardinality());//cardinality is //used to print the bits//of b1 and b2 if(sc.hasNextLine()){sc.nextLine();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java BitSet
You are viewing a single comment's thread. Return to all comments →