Java by API/java.util/BitSet

Материал из Java эксперт
Перейти к: навигация, поиск

BitSet: and(BitSet anotherBitSet)

 
/*
 * Output:
Initial pattern in bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
Initial pattern in bits2: 
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}
bits2 AND bits1: 
{2, 4, 6, 8, 12, 14}
bits2 OR bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
bits2 XOR bits1: 
{}  
 */
import java.util.BitSet;
public class MainClass {
  public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);
    // set some bits
    for (int i = 0; i < 16; i++) {
      if ((i % 2) == 0)
        bits1.set(i);
      if ((i % 5) != 0)
        bits2.set(i);
    }
    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);
    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);
    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);
    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
  }
}





BitSet: andNot(BitSet set)

 
import java.util.BitSet;
public class Main {
 public static void main (String args[]) {
    BitSet bites = new BitSet();
    bites.set(0);
    bites.set(1);
    bites.set(2);
    bites.set(3);
    System.out.println(bites);
 
    bites.andNot(bites);
    System.out.println(bites);
 }
}





BitSet: clone()

 
import java.util.BitSet;
public class Main {
  public static void main(String args[]) {
    BitSet bites = new BitSet();
    bites.set(0);
    bites.set(1);
    bites.set(2);
    bites.set(3);
    BitSet bitSetClone = (BitSet) bites.clone();
    System.out.println(bites);
    System.out.println(bitSetClone);
  }
}





BitSet: get(int bitIndex)

 
/*
 * Bit 65 is true
 */
import java.util.BitSet;
public class MainClass {
  public static void main(String[] argv) {
    BitSet bs = new BitSet();
    bs.set(65);
    System.out.println("Bit 65 is " + bs.get(65));
  }
}





BitSet: length()

 
import java.util.BitSet;
public class Main {
  public static void main(String args[]) {
    BitSet bites = new BitSet();
    bites.set(0);
    bites.set(1);
    bites.set(2);
    bites.set(3);
    System.out.println(bites.size());
    System.out.println(bites.length());
  }
}





BitSet: or(BitSet anotherBitSet)

 
/*
 * Output:
Initial pattern in bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
Initial pattern in bits2: 
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}
bits2 AND bits1: 
{2, 4, 6, 8, 12, 14}
bits2 OR bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
bits2 XOR bits1: 
{}  
 */
import java.util.BitSet;
public class MainClass {
  public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);
    // set some bits
    for (int i = 0; i < 16; i++) {
      if ((i % 2) == 0)
        bits1.set(i);
      if ((i % 5) != 0)
        bits2.set(i);
    }
    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);
    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);
    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);
    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
  }
}





BitSet: set(int bitIndex)

 
/*
 * Output:
Initial pattern in bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
Initial pattern in bits2: 
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}
bits2 AND bits1: 
{2, 4, 6, 8, 12, 14}
bits2 OR bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
bits2 XOR bits1: 
{}  
 */
import java.util.BitSet;
public class MainClass {
  public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);
    // set some bits
    for (int i = 0; i < 16; i++) {
      if ((i % 2) == 0)
        bits1.set(i);
      if ((i % 5) != 0)
        bits2.set(i);
    }
    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);
    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);
    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);
    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
  }
}





BitSet: size()

 
import java.util.BitSet;
public class Main {
  public static void main(String args[]) {
    BitSet bites = new BitSet();
    bites.set(0);
    bites.set(1);
    bites.set(2);
    bites.set(3);
    System.out.println(bites.size());
    System.out.println(bites.length());
  }
}





BitSet: xor(BitSet anotherBitSet)

 
/*
 * Output:
Initial pattern in bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
Initial pattern in bits2: 
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}
bits2 AND bits1: 
{2, 4, 6, 8, 12, 14}
bits2 OR bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
bits2 XOR bits1: 
{}  
 */
import java.util.BitSet;
public class MainClass {
  public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);
    // set some bits
    for (int i = 0; i < 16; i++) {
      if ((i % 2) == 0)
        bits1.set(i);
      if ((i % 5) != 0)
        bits2.set(i);
    }
    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);
    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);
    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);
    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
  }
}





new BitSet(int size)

 
/*
 * Output:
Initial pattern in bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
Initial pattern in bits2: 
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}
bits2 AND bits1: 
{2, 4, 6, 8, 12, 14}
bits2 OR bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}
bits2 XOR bits1: 
{}  
 */
import java.util.BitSet;
public class MainClass {
  public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);
    // set some bits
    for (int i = 0; i < 16; i++) {
      if ((i % 2) == 0)
        bits1.set(i);
      if ((i % 5) != 0)
        bits2.set(i);
    }
    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);
    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);
    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);
    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
  }
}