Java BitSet get(int bitIndex) Method



Description

The Java BitSet get(int bitIndex) method returns the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set in this BitSet; otherwise, the result is false.

Declaration

Following is the declaration for java.util.BitSet.get() method

public boolean get(int bitIndex)

Parameters

bitIndex − the bit index.

Return Value

This method returns the value of the bit with the specified index.

Exception

IndexOutOfBoundsException − if the specified index is negative.

Java BitSet get(int fromIndex, int toIndex) Method

Description

The java.util.BitSet.get(int fromIndex,int toIndex) method returns a new BitSet composed of bits from this BitSet from fromIndex (inclusive) to toIndex (exclusive).

Declaration

Following is the declaration for java.util.BitSet.get() method

public BitSet get(int fromIndex,int toIndex)

Parameters

  • fromIndex − index of the first bit to include.

  • toIndex − index after the last bit to include.

Return Value

This method returns a new BitSet from a range of this BitSet.

Exception

IndexOutOfBoundsException − if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

Getting a Bit By Index from a BitSet Example

The following example shows the usage of Java BitSet get(bitIndex) method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using get(bitIndex) method we're printing value of one true bit and one false bit.

package com.tutorialspoint;

import java.util.BitSet;

public class BitSetDemo {
   public static void main(String[] args) {

      // create a bitset
      BitSet bitset = new BitSet();

      // assign values to bitset
      bitset.set(0, 6, true);

      // print the set
      System.out.println("Bitset:" + bitset);

      // print the value at index 2
      System.out.println(bitset.get(2));

      // print the value at index 7
      System.out.println(bitset.get(7));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Bitset:{0, 1, 2, 3, 4, 5}
Bitset:{0, 1, 3, 4, 5}

Getting Multiple Bits By Indexes from a BitSet Example

The following example shows the usage of Java BitSet get(fromIndex, toIndex) method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using get(fromIndex, toIndex) method we're getting a smaller bitset and printing the same.

package com.tutorialspoint;

import java.util.BitSet;

public class BitSetDemo {
   public static void main(String[] args) {

      // create a bitset
      BitSet bitset = new BitSet();

      // assign values to bitset
      bitset.set(0, 6, true);

      // print the set
      System.out.println("Bitset:" + bitset);     

      // print the bitset from 3 to 6
      System.out.println("Bitset:" + bitset.get(3,6));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Bitset:{0, 1, 2, 3, 4, 5}
Bitset:{0, 1, 2}

Getting a Bit By Index from a BitSet of Bytes Example

The following example shows the usage of Java BitSet get(bitIndex) method. We're creating two BitSets using byte[] and using get(bitIndex) method we're printing value of one true bit and one false bit.

package com.tutorialspoint;

import java.util.BitSet;

public class BitSetDemo {
   public static void main(String[] args) {

      // create a bitset
      BitSet bitset = BitSet.valueOf(new byte[] { 0, 1, 2, 3, 4, 5 });

      // print the set
      System.out.println("Bitset:" + bitset);

      // print the value at index 17
      System.out.println(bitset.get(17));

      // print the value at index 18
      System.out.println(bitset.get(18));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Bitset:{8, 17, 24, 25, 34, 40, 42}
true
false
java_util_bitset.htm
Advertisements