Home » Java programming language

Java BitSet nextClearBit() Method with Example

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

BitSet Class nextClearBit() method

  • nextClearBit() method is available in java.util package.
  • nextClearBit() method is used to retrieve the index of the first bit that is set to false that occurs or searching starts after the given index.
  • nextClearBit() method is a non-static method, so it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • nextClearBit() method may throw an exception at the time of checking the given index.
    IndexOutOfBoundsException: This exception may throw when the given index is less than 0.

Syntax:

    public int nextClearBit(int src_in);

Parameter(s):

  • int src_in – represent the index to start searching from.

Return value:

The return type of this method is int, it returns the index of the first bit set to false or next clear bit.

Example:

// Java program to demonstrate the example 
// of int nextClearBit(int src_in) method of BitSet.

import java.util.*;

public class NextClearBitOfBitSet {
    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);

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

        // By using nextClearBit() method is to return
        // the first bit set to false
        int next_clr_bit = bs.nextClearBit(2);

        // Display next_clr_bit
        System.out.println("bs.nextClearBit(2): " + next_clr_bit);
    }
}

Output

bs: {10, 20, 30, 40, 50}
bs.nextClearBit(2): 2



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.