Java.util.BitSet.set() method in Java

There are four variants of set() method.This article depicts about all of them, as follows:

1. set(int Index) : This method sets the bit at the specified index to true i.e adds a value.

Declaration : 
public void set(int bitIndex)
Parameters : 
     Index :  a bit index.
Result : 
  This method does not return a value.
Exception : 
     IndexOutOfBoundsException :  if the specified index is negative.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working
// of set() in BitSet
import java.util.*;
public class BitSet1 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(5);
  
        // assigning value using set()
        bset.set(3);
        bset.set(5);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

chevron_right


Output:



The constructed bitset is : {3, 5}

2. set(int num, boolean value) : This method sets the bit at the specified index to the specified value, if boolean value is true, value is added, else not added.

Declaration : 
  public void set(int num, boolean value)
Parameters : 
   num :  a bit value.
   value :  a boolean value to set.
Return Value
    This method does not return a value.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working
// of set(num, value) in BitSet
import java.util.*;
public class BitSet2 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(5);
  
        // assigning value using set(num, value)
        // adds 3
        bset.set(3, true);
  
        // doesn't add 5
        bset.set(5, false);
        bset.set(7, true);
        bset.set(4, false);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

chevron_right


Output:

The constructed bitset is : {3, 7}

3. set(int fromnum, int tonum) : This method sets the bits from the specified fromnum(inclusive) to the specified tonum(exclusive) to true, i.e adds the values fromnum to tonum-1.

Declaration : 
   public void set(int fromnum, int tonum)
Parameters : 
    fromnum :  value to start insert.
    tonum :  value to end insertion.
Return Value : 
   This method does not return a value.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working
// of set(fromnum, tonum) in BitSet
import java.util.*;
public class BitSet3 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(9);
  
        // assigning value using set(fromnum, tonum)
        // adds 3 to 8
        bset.set(3, 9);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

chevron_right


Output:

The constructed bitset is : {3, 4, 5, 6, 7, 8}

4. set(int fromnum, int tonum, boolean value) : This method sets the bits from the specified fromnum (inclusive) to the specified tonum (exclusive) to the specified value i.e inserts fromnum to tonum-1 if boolean value passed is true, else doesn’t insert.

Declaration : 
  public void set(int fromnum, int tonum, boolean value)
Parameters : 
   fromnum :  num to start inserting.
   tonum :  last number of insertion.
   value :  value to set the selected bits to.
Return Value : 
   This method does not return a value.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working
// of set(fromnum, tonum, value) in BitSet
import java.util.*;
public class BitSet4 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(9);
  
        // assigning value using set(fromnum, tonum, value)
        // doesn't adds 3 to 8
        bset.set(3, 9, false);
  
        // assigning value using set(fromnum, tonum, value)
        // adds 5 to 10
        bset.set(5, 11, true);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

chevron_right


Ouput:

The constructed bitset is : {5, 6, 7, 8, 9, 10}

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.