---
title: "Bitwise and Bit Shift Operators"  
description: "The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this"  
author: "ANkita gupta"  
published: 2015-05-15  
updated: 2018-02-21  
canonical: https://www.mindstick.com/blog/10886/bitwise-and-bit-shift-operators  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Bitwise and Bit Shift Operators

The Java [programming language](https://www.mindstick.com/articles/329035/5-most-in-demand-programming-languages-to-consider-for-your-app-development-in-2022) also provides [operators](https://www.mindstick.com/articles/716/php-operators) that perform bitwise and bit shift [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git) on integral types. The operators discussed in this [section](https://yourviews.mindstick.com/view/297/reservation-for-economically-weaker-section) are less commonly used. Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist.

The unary bitwise complement [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

The bitwise & operator performs a bitwise AND operation.

The bitwise ^ operator performs a bitwise exclusive OR operation.

The bitwise | operator performs a bitwise inclusive OR operation.

The following [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program), BitDemo, uses the bitwise AND operator to print the number "2" to [standard](https://www.mindstick.com/articles/23223/naming-convention-or-coding-standard) output.

```
class BitDemo {
    public static void main(String[] args) {
        int bitmask = 0x000F;
        int val = 0x2222;
        // prints "2"
        System.out.println(val & bitmask);    }}
```

The following quick [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) summarizes the operators supported by the Java programming language.

##### Simple Assignment Operator

```
=       Simple assignment operator
```

##### Arithmetic Operators

```
+       Additive operator (also used
        for String concatenation)
-       Subtraction operator
*       Multiplication operator
/       Division operator
%       Remainder operator
```

##### Unary Operators

```
+ Unary plus operator; indicates
        positive value (numbers are
        positive without this, however)
- Unary minus operator; negates
        an expression
++ Increment operator; increments
        a value by 1
-- Decrement operator; decrements
        a value by 1
! Logical complement operator;
        inverts the value of a boolean
```

##### Equality and Relational Operators

```
==      Equal to
!=      Not equal to
>       Greater than
>=      Greater than or equal to
<       Less than
<=      Less than or equal to
```

##### Conditional Operators

```
&&      Conditional-AND
||      Conditional-OR
?:      Ternary (shorthand for
        if-then-else statement)
```

##### Type Comparison Operator

```
instanceof      Compares an object to
                a specified type
```

##### Bitwise and Bit Shift Operators

```
~       Unary bitwise complement
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR
```

---

Original Source: https://www.mindstick.com/blog/10886/bitwise-and-bit-shift-operators

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
