Home » Java programming language

Java BitSet flip() Method with Example

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

BitSet Class flip() method

Syntax:

    public void flip(int bit_in);
    public void flip(int st_in, int en_in);
  • flip() method is available in java.util package.
  • flip(int bit_in) method is used to set the bit at the given bit index to the reverse of its current set value.
  • flip(int st_in, int en_in) method is used to set each bit in a range at the given st_in(starting index) and en_in(ending index) to the reverse of its current set value.
  • flip(int bit_in) method may throw an exception at the time of assigning index.
    IndexOutOfBoundsException: This exception may throw when the given index is not in a range.
  • flip(int st_in, int en_in) method may throw an exception at the time of assigning index.
    IndexOutOfBoundsException: This exception may throw when st_in, en_in are 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, flip(int bit_in)
    • int bit_in – represents the index of the bit in this BitSet to flip.
  • In the third case, flip(int st_in, int en_in)
    • int st_in – represent the index of the starting bit to flip.
    • int en_in – represents the index after the ending bit to flip.

Return value:

In all the cases, the return type of the method is void, it returns nothing.

Example:

// Java Program to demonstrate the example of
// void flip() method of BitSet

import java.util.*;

public class Flip {
    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 flip(int bit) method is to
        // flip the matches bit from bitset
        bs.flip(30);

        // Display BitSet 
        System.out.println("bs.flip(30): " + bs);

        // By using flip(int st_bit , int en_bit) method 
        // is to flip the bit starting at st_bit and ending at 
        // before en_bit from bitset
        bs.flip(20, 40);

        // Display BitSet 
        System.out.println("bs.flip(20,40): " + bs);
    }
}

Output

bs: {10, 20, 30, 40, 50}
bs.flip(30): {10, 20, 40, 50}
bs.flip(20,40): {10, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 50}





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.