
- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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.
Example 1
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)); } }
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}
Example 2
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)); } }
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}
Example 3
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)); } }
Let us compile and run the above program, this will produce the following result −
Bitset:{8, 17, 24, 25, 34, 40, 42} true false