Java BitSet previousSetBit() methodLast Updated : 17 Mar 2025 The previousSetBit(int fromIndex) method of Java BitSet class returns the index of the nearest bit which is set to true that occurs on or before the specified index. It returns -1 if the specified index is negative or no such set bit exists in the BitSet. Syntax:Parameter:
Returns:This method returns the index of the previous set bit, or -1 if there is no such set bit exists. Exception:IndexOutOfBoundsException - throw an exception if the specified index is negative. Compatibility Version:Java 1.7 and above Example 1Output:
bitset: {0, 1, 4, 6, 7}
previous set bit on or before 4: 4
previous set bit on or before 5: 4
Example 2The previousSetBit(int fromIndex) method returns -1 if the specified index is negative. Output:
bitset: {0, 1, 3, 4}
previous set bit previous to -1: -1
Next TopicJava-bitset-previousclearbit-method |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India