Home » Java programming language

Java BitSet get() Method with Example

BitSet Class get() method: Here, we are going to learn about the get() method of BitSet Class with its syntax and example.
Submitted by Preeti Jain, on January 21, 2020

BitSet Class get() method

Syntax:

    public boolean get(int bit_in);
    public BitSet get(int st_in, int en_in);
  • get() method is available in java.util package.
  • get(int bit_in) method is used to return the value of the given bit indices (bit_in). It returns true when the bit with the given index is set by using the set() method.
  • get(int st_in, int en_in) method is used to returns a subset consisted of bits from this BitSet from the given range st_in(starting index) and en_in(ending index).
  • get(int bit_in) method may throw an exception at the time of checking index.
    IndexOutOfBoundsException: This exception may throw when the given index is less than 0.
  • get(int st_in, int en_in) method may throw an exception at the time of checking exception.
    IndexOutOfBoundsException: This exception may throw when st_in or en_in is less than 0 or st_in > en_in.
  • These are non-static methods, so it is accessible with the class object and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the second case, get(int bit_in)
    • int bit_in – represents the bit index.
  • In the third case, get(int st_in, int en_in)
    • int st_in – represent the starting bit(st_in) to conclude.

Return value:

In the first case, boolean get(bit_in): The return type of the method is boolean, it returns true when it returns the value of the bit of the given index.

In the second case, BitSet get(int st_in, int en_in), it returns BitSet of the given range (st_in & en_in).

Example:

// Java program to demonstrate the example 
// of get() method of BitSet.

import java.util.*;

public class GetOfBitSet {
    public static void main(String[] args) {
        // create an object of BitSet
        BitSet bs = new BitSet(10);

        // By using set() method is to set
        // the values in BitSet 
        bs.set(10);
        bs.set(20);
        bs.set(30);
        bs.set(40);
        bs.set(50);
        bs.set(60);
        bs.set(70);
        bs.set(80);

        // Display Bitset
        System.out.println("bs: " + bs);

        // By using get(40) method is used to 
        // check the given bit exists in this BitSet or not
        boolean status = bs.get(40);

        // Display status
        System.out.println("bs.get(40): " + status);

        // By using get(40,60) method is used to 
        // check the given set of bits exists in this
        // BitSet or not

        // Display Bitset
        System.out.println("bs.get(40,60): " + bs.get(40, 60));
    }
}

Output

bs: {10, 20, 30, 40, 50, 60, 70, 80}
bs.get(40): true
bs.get(40,60): {0, 10}






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.