BitSet Example

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - BitSet Example

This Java example program shows how to deal with BitSet.

[edit] Java Source Code

  • Package: example.bitset
  • File: BitOHoney.java
  1. package example.bitset;
  2.  
  3. import java.util.BitSet;
  4.  
  5. public class BitOHoney {
  6.     public static void main(String args[]) {
  7. 	String names[] = { "Java", "Source", "and", "Support" };
  8. 	BitSet bits = new BitSet();
  9. 	for (int i = 0, n = names.length; i < n; i++) {
  10. 	    if ((names[i].length() % 2) == 0) {
  11. 		bits.set(i);
  12. 	    }
  13. 	}
  14. 	System.out.println(bits);
  15. 	System.out.println("Size : " + bits.size());
  16. 	System.out.println("Length: " + bits.length());
  17. 	for (int i = 0, n = names.length; i < n; i++) {
  18. 	    if (!bits.get(i)) {
  19. 		System.out.println(names[i] + " is odd");
  20. 	    }
  21. 	}
  22. 	BitSet bites = new BitSet();
  23. 	bites.set(0);
  24. 	bites.set(1);
  25. 	bites.set(2);
  26. 	bites.set(3);
  27. 	bites.andNot(bits);
  28. 	System.out.println(bites);
  29.     }
  30. }

[edit] What Result You Can Get

Run the program, you will get:

{0, 1}
Size : 64
Length: 2
and is odd
Support is odd
{2, 3}

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools