How to use BitSet with bits in Java

5 Answers

0 votes
import java.util.*; 

public class MyClass {
    public static int TOTAL_BITS = 4;
    
    public static void set_random_bits(BitSet sb) {
        Random r = new Random();
        
        for (int i = 0; i < TOTAL_BITS / 2; i++)
            sb.set(r.nextInt(TOTAL_BITS));
    }
    public static void print_bits(BitSet sb) {
        for (int i = 0; i < TOTAL_BITS; i++) {
             System.out.print(sb.get(i) ? "1" : "0");
        }
        System.out.println();
    }
    public static void main(String args[]) {
        BitSet bs1 = new BitSet(TOTAL_BITS);
        BitSet bs2 = new BitSet(TOTAL_BITS); 

        set_random_bits(bs1);
        set_random_bits(bs2);
        
        print_bits(bs1);
        print_bits(bs2); 
        
        bs1.andNot(bs2);
 
        print_bits(bs1);
        System.out.println(bs1); 
    }
}
 
 
 
/*
run:
 
1100
1000
0100
{1}
 
*/

 



answered Oct 11, 2019 by avibootz
0 votes
import java.util.*; 

public class MyClass {
    public static int TOTAL_BITS = 4;
    
    public static void set_random_bits(BitSet sb) {
        Random r = new Random();
        
        for (int i = 0; i < TOTAL_BITS / 2; i++)
            sb.set(r.nextInt(TOTAL_BITS));
    }
    public static void print_bits(BitSet sb) {
        for (int i = 0; i < TOTAL_BITS; i++) {
             System.out.print(sb.get(i) ? "1" : "0");
        }
        System.out.println();
    }
    public static void main(String args[]) {
        BitSet bs1 = new BitSet(TOTAL_BITS);
        BitSet bs2 = new BitSet(TOTAL_BITS); 

        set_random_bits(bs1);
        set_random_bits(bs2);
        
        print_bits(bs1);
        print_bits(bs2); 
        
        bs1.and(bs2); 
 
        print_bits(bs1);
        System.out.println(bs1); 
    }
}
 
 
 
/*
run:
 
1001
0011
0001
{3}
 
*/

 



answered Oct 11, 2019 by avibootz
0 votes
import java.util.*; 

public class MyClass {
    public static int TOTAL_BITS = 4;
    
    public static void set_random_bits(BitSet sb) {
        Random r = new Random();
        
        for (int i = 0; i < TOTAL_BITS / 2; i++)
            sb.set(r.nextInt(TOTAL_BITS));
    }
    public static void print_bits(BitSet sb) {
        for (int i = 0; i < TOTAL_BITS; i++) {
             System.out.print(sb.get(i) ? "1" : "0");
        }
        System.out.println();
    }
    public static void main(String args[]) {
        BitSet bs1 = new BitSet(TOTAL_BITS);
        BitSet bs2 = new BitSet(TOTAL_BITS); 

        set_random_bits(bs1);
        set_random_bits(bs2);
        
        print_bits(bs1);
        print_bits(bs2); 
        
        if (bs1.equals(bs2)) 
           System.out.println("Equals"); 
        else
           System.out.println("Not Equals"); 
    }
}
 
 
 
/*
run:
 
0110
0101
Not Equals
 
*/

 



answered Oct 11, 2019 by avibootz
0 votes
import java.util.*; 

public class MyClass {
    public static int TOTAL_BITS = 4;
    
    public static void set_random_bits(BitSet sb) {
        Random r = new Random();
        
        for (int i = 0; i < TOTAL_BITS / 2; i++)
            sb.set(r.nextInt(TOTAL_BITS));
    }
    public static void print_bits(BitSet sb) {
        for (int i = 0; i < TOTAL_BITS; i++) {
             System.out.print(sb.get(i) ? "1" : "0");
        }
        System.out.println();
    }
    public static void main(String args[]) {
        BitSet bs1 = new BitSet(TOTAL_BITS);
        BitSet bs2 = new BitSet(TOTAL_BITS); 

        set_random_bits(bs1);
        set_random_bits(bs2);
        
        print_bits(bs1);
        print_bits(bs2); 
        
        bs1.or(bs2);
        
        print_bits(bs1);
        System.out.println(bs1); 
    }
}
 
 
 
/*
run:
 
1100
1001
1101
{0, 1, 3}
 
*/

 



answered Oct 11, 2019 by avibootz
0 votes
import java.util.*; 

public class MyClass {
    public static int TOTAL_BITS = 4;
    
    public static void set_random_bits(BitSet sb) {
        Random r = new Random();
        
        for (int i = 0; i < TOTAL_BITS / 2; i++)
            sb.set(r.nextInt(TOTAL_BITS));
    }
    public static void print_bits(BitSet sb) {
        for (int i = 0; i < TOTAL_BITS; i++) {
             System.out.print(sb.get(i) ? "1" : "0");
        }
        System.out.println();
    }
    public static void main(String args[]) {
        BitSet bs1 = new BitSet(TOTAL_BITS);
        BitSet bs2 = new BitSet(TOTAL_BITS); 

        set_random_bits(bs1);
        set_random_bits(bs2);
        
        print_bits(bs1);
        print_bits(bs2); 
        
        bs1.xor(bs2);
        
        print_bits(bs1);
        System.out.println(bs1); 
    }
}
 
 
 
/*
run:
 
0110
1000
1110
{0, 1, 2}
 
*/

 



answered Oct 11, 2019 by avibootz

Related questions

1 answer 121 views
121 views asked Oct 12, 2019 by avibootz
1 answer 125 views
125 views asked Oct 11, 2019 by avibootz
5 answers 275 views
275 views asked Oct 11, 2019 by avibootz
1 answer 131 views
1 answer 126 views
1 answer 133 views
5 answers 289 views
289 views asked Dec 6, 2019 by avibootz
...