Java.Core.What bitwise operations do you know?

Bitwise Operators in Java

Bitwise operators in Java operate on individual bits of integer values. These operators are useful for low-level programming, optimizations, and working with binary numbers.


1. List of Bitwise Operators

OperatorNameDescriptionExample (5 & 3)
&Bitwise ANDSets bits to 1 if both bits are 15 & 3 → 1 (0101 & 0011 → 0001)
|Bitwise ORSets bits to 1 if at least one bit is 1
^Bitwise XOR (Exclusive OR)Sets bits to 1 if bits are different5 ^ 3 → 6 (0101 ^ 0011 → 0110)
~Bitwise NOT (Complement)Inverts all bits (1 → 0, 0 → 1)~5 → -6 (flips 0101 to 1010, two’s complement)
<<Left ShiftShifts bits left, multiplying by 2^n5 << 1 → 10 (0101 → 1010)
>>Right ShiftShifts bits right, dividing by 2^n (sign preserved)5 >> 1 → 2 (0101 → 0010)
>>>Unsigned Right ShiftShifts bits right, filling zeros (0)-5 >>> 1 → large positive number

2. Bitwise AND (&)

  • Performs AND operation on each bit.
  • The result is 1 only if both bits are 1.

Example

public class BitwiseAndExample {
    public static void main(String[] args) {
        int a = 5;  // 0101
        int b = 3;  // 0011
        System.out.println("a & b: " + (a & b)); // 0001 → 1
    }
}

5 & 3 compares each bit:

  0101  (5)
& 0011  (3)
--------
  0001  (1)

3. Bitwise OR (|)

  • Performs OR operation on each bit.
  • The result is 1 if at least one bit is 1.

Example

public class BitwiseOrExample {
    public static void main(String[] args) {
        int a = 5;  // 0101
        int b = 3;  // 0011
        System.out.println("a | b: " + (a | b)); // 0111 → 7
    }
}

5 | 3 compares each bit:

  0101  (5)
| 0011  (3)
--------
  0111  (7)

4. Bitwise XOR (^)

  • Returns 1 only if bits are different.

Example

public class BitwiseXorExample {
    public static void main(String[] args) {
        int a = 5;  // 0101
        int b = 3;  // 0011
        System.out.println("a ^ b: " + (a ^ b)); // 0110 → 6
    }
}

5 ^ 3 compares each bit:

  0105  (5)
^ 0011  (3)
--------
  0110  (6)

5. Bitwise NOT (~) (Complement)

  • Flips all bits (1 → 0, 0 → 1).
  • Uses two’s complement to represent negative numbers.

Example

public class BitwiseNotExample {
    public static void main(String[] args) {
        int a = 5;  // 0101
        System.out.println("~a: " + (~a)); // 1010 → -6
    }
}

~5 flips bits and converts to negative:

  0000 0000 0000 0000 0000 0000 0000 0101  (5)
~ ----------------------------------------
  1111 1111 1111 1111 1111 1111 1111 1010  (-6 in two's complement)

Why -6?

  • Java uses two’s complement representation for negative numbers.

6. Left Shift (<<)

  • Shifts bits left, filling with 0s.
  • Multiplies the number by 2^n.

Example

public class LeftShiftExample {
    public static void main(String[] args) {
        int a = 5;  // 0101
        System.out.println("a << 1: " + (a << 1)); // 1010 → 10
    }
}

5 << 1 shifts left:

  0000 0101  (5)
<< 1
  0000 1010  (10)

Formula:a << na * 2^n

7. Right Shift (>>)

  • Shifts bits right, keeping the sign bit.
  • Divides the number by 2^n.

Example

public class RightShiftExample {
    public static void main(String[] args) {
        int a = 5;  // 0101
        System.out.println("a >> 1: " + (a >> 1)); // 0010 → 2
    }
}

5 >> 1 shifts right:

  0000 0101  (5)
>> 1
  0000 0010  (2)

Formula: a >> na / 2^n


8. Unsigned Right Shift (>>>)

  • Shifts bits right, filling zeros (0).
  • Works differently for negative numbers.

Example

public class UnsignedRightShiftExample {
    public static void main(String[] args) {
        int a = -5;
        System.out.println("a >>> 1: " + (a >>> 1));
    }
}

-5 >>> 1 shifts right, filling with zeros:

  1111 1111 1111 1111 1111 1111 1111 1011  (-5 in binary)
>>> 1
  0111 1111 1111 1111 1111 1111 1111 1101  (large positive number)

9. Bitwise Operators vs. Logical Operators

OperatorTypeBehavior
&Bitwise ANDCompares each bit (1 & 1 → 1)
&&Logical ANDEvaluates Boolean expressions (true && false → false)
``Bitwise OR
``

Bitwise operators work on binary numbers.
Logical operators work on Boolean values (true/false).


10. Final Thoughts

OperatorOperationEffect
&Bitwise AND1 & 1 → 1, 0 & 1 → 0
|Bitwise OR
^Bitwise XOR1 ^ 0 → 1, 1 ^ 1 → 0
~Bitwise NOTInverts bits (~5 → -6)
<<Left ShiftMultiplies by 2^n
>>Right ShiftDivides by 2^n (keeps sign)
>>>Unsigned Right ShiftDivides by 2^n (fills with 0)
This entry was posted in Без рубрики. Bookmark the permalink.