# Performing Bitwise Operations on a Bit Vector : Binary Bit « Language Basics « Java

Home
Java
 1 2D Graphics GUI 2 3D 3 Advanced Graphics 4 Ant 5 Apache Common 6 Chart 7 Class 8 Collections Data Structure 9 Data Type 10 Database SQL JDBC 11 Design Pattern 12 Development Class 13 EJB3 14 Email 15 Event 16 File Input Output 17 Game 18 Generics 19 GWT 20 Hibernate 21 I18N 22 J2EE 23 J2ME 24 JDK 6 25 JNDI LDAP 26 JPA 27 JSP 28 JSTL 29 Language Basics 30 Network Protocol 31 PDF RTF 32 Reflection 33 Regular Expressions 34 Scripting 35 Security 36 Servlets 37 Spring 38 Swing Components 39 Swing JFC 40 SWT JFace Eclipse 41 Threads 42 Tiny Application 43 Velocity 44 Web Services SOA 45 XML
 Java » Language Basics » Binary Bit Screenshots
Performing Bitwise Operations on a Bit Vector
 ```   import java.util.BitSet; public class Main {   public static void main(String[] argv) throws Exception {     // Create the bitset     BitSet bits = new BitSet();     // Set a bit on     bits.set(2);      // Retrieving the value of a bit     boolean b = bits.get(0);      b = bits.get(2);      // Clear a bit     bits.clear(1);     // Setting a range of bits     BitSet bits2 = new BitSet();     bits2.set(1, 4);      // And'ing two bitsets     bits.and(bits2);      // Xor'ing two bitsets     bits.xor(bits2);      // Flip all bits in the bitset     bits.flip(0, bits.length());      // Andnot'ing two bitsets     bits.andNot(bits2);      // Or'ing two bitsets     bits.or(bits2);    } }            ```
Related examples in the same category
 1 Utility for byte swapping of all java data types 2 Bitwise Demo 3 Binary Digits 4 Using the bitwise operators 5 Bitwise complement (~): inverts ones and zeroes in a number 6 Convert a number to negative and back 7 Bitwise AND (&) 8 Bitwise OR (|) 9 Bitwise XOR (^) 10 Left shift (<<) 11 Converting Between a BitSet and a Byte Array 12 Returns a byte array of at least length 1 13 Shift to the left 14 Signed shift to the right 15 Unsigned shift to the right 16 Bit-level unpacking of floating-point data 17 Gets the a single bit of the target. 18 Returns 16 bits from the long number. 19 Sets a specific bit of an int. 20 Fuses the lower 16 bits of two ints. 21 Class to represent unsigned 32-bit numbers. 22 A list of bits. 23 Gets the specified bit (0-31) from the integer argument. 24 Sets the specified bit (0-31) in the integer argument. 25 Clears a range of bits in the specified integer.