Performing Bitwise Operations on a Bit Vector : Bitwise Operators « Operators « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Operators » Bitwise Operators 
3.5.18.Performing Bitwise Operations on a Bit VectorPrevious/Next
import java.util.BitSet;

public class Main {
  public static void main(String[] argvthrows 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(14)

    // 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)
  }
}
3.5.Bitwise Operators
3.5.1.The Bitwise Operators can be applied to the integer types, long, int, short, char, and byte.
3.5.2.The Bitwise Logical Operators
3.5.3.Bitwise AND (&)
3.5.4.Bitwise OR (|)
3.5.5.Bitwise XOR (^)
3.5.6.Left shift (<<)
3.5.7.Bitwise complement (~): inverts ones and zeros in a number
3.5.8.Demonstrate the bitwise logical operators
3.5.9.All bitwise operators in action
3.5.10.Bitwise Operator Assignments
3.5.11.The Left Shift
3.5.12.Left shifting as a quick way to multiply by 2
3.5.13.The Right Shift
3.5.14.The Unsigned Right Shift
3.5.15.Signed shift to the right
3.5.16.Unsigned shifting a byte value.
3.5.17.Convert a number to negative and back
3.5.18.Performing Bitwise Operations on a Bit Vector
3.5.19.Converting Between a BitSet and a Byte Array
3.5.20.Returns a byte array of at least length 1
3.5.21.Use bitwise operator to create hash code
3.5.22.Operations on bit-mapped fields.
3.5.23.Represents a collection of 64 boolean (on/off) flags.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.