3. • As is well known, numbers are stored and operated as
binaries inside a computer. So, bit manipulation, as named,
is to manipulate binary bits of a number directly.
E.g. 6 & 11 = 0x110 & 0x1011 = 0x0001 = 2
• Java provides extensive bit-manipulation capabilities for
programmers who need to get down to the “bits-and-bytes”
level.
• Computers represent all data internally as sequences of
bits. Each bit can assume the value 0 or the value 1.
Bit Manipulation??
4. Bitwise Operators
Bitwise operators perform operations
on the bits of their operands. The
operands can only be byte, short, int,
long or char data types.
For example, if an operand is the
number 48, the bitwise operator will
perform its operation on the binary
representation of 48 (i.e., 110000).
5. Bitwise AND(&):
-set to 1 if both bits are 1.
Bitwise OR(|):
-set to 1 if atleast one bit is 1.
6. Bitwise exclusive OR:
-set to one if only 1 bit is set to 1.
Bitwise ~:
-All 1’s become 0 ‘s and all 0’s become 1’s.
7. The left shift operator will shift the bits towards left for the
given number of times.
int a=2<<1;
Let’s take the binary representation of 2 assuming int is 1
byte for simplicity.
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 0 1 0
Now shifting the bits towards left for 1 time, will give the
following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Left Shift <<:NOTE:
Now the
result in
decimal is 4.
If you left
shift like
2<<2, then it
will give the
result as 8.
Therefore
left shifting 1
time, is
equal to
multiplying
the value by
2.
8. The right shift operator will shift the bits towards right for the given
number of times.
int a=8>>1;
Let’s take the binary representation of 8 assuming int is 1 byte for
simplicity.
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 1 0 0 0
Now shifting the bits towards right for 1 time, will give the following
results
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Right Shift >>:
Now the
result in
decimal is
4. Right
shifting 1
time, is
equivalent
to dividing
the value
by 2.
NOTE:
11. BITSET CONSTRUCTOR:
Sno Constructor Description
1. BitSet( ) This constructor creates a new bit
set.
2. BitSet(int size) This constructor allows you to
specify its initial size, i.e., the
number of bits that it can hold. All
bits are initialized to zero
12. Sno Method Description
1. void and(BitSet set) This method performs a logical AND of this target
bit set with the argument bit set.
2. void andNot(BitSet
set)
This method clears all of the bits in this BitSet
whose corresponding bit is set in the specified
BitSet.
3. int cardinality( ) This method returns the number of bits set to true
in this BitSet.
4. void clear( ) Zeros all bits.
5. void clear(int index) Zeros the bit specified by index.
BITSET METHODS:
13. 6. void set(int bitIndex) Sets the bit specified by index to true.
7. void set(int bitIndex,
boolean v)
This method sets the bit at the specified index to
the specified value.
8. void set(int startIndex, int
endIndex)
Sets the bits from(inclusive) startIndex to
endIndex (exclusive)to true.
9. String toString( ) Returns the string equivalent of the bit set.
10. void xor(BitSet bitSet) This method performs a logical XOR of this bit
set with the bit set argument.
14. 11. Object clone( ) This method clones the BitSet and
produces a new BitSet that is equal to it.
12. int hashCode() This method returns a hash code value
for this bit set.
13. void flip(int index) method sets the bit at the specified index
to the complement of its current value.
14. int length() This method returns the "logical size" of
this BitSet: the index of the highest set bit
in the BitSet plus one.
15. boolean get(int index) This method returns the value of the bit
with the specified index.
16. boolean intersects(BitSet
set)
This method returns true if the specified
BitSet has any bits set to true that are
also set to true in this BitSet.
15. import java.util.BitSet;
public class BitSetDemo
{
public static void main(String args[])
{
BitSet bitset1=new BitSet(8);
BitSet bitset2=new BitSet(8);
// assign values to bitset1
bitset1.set(0);
bitset1.set(1);
bitset1.set(2);
bitset1.set(3);
bitset1.set(4);