---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 21 Edition"
java_version: 21
chapter: "Chapter 15. Expressions"
section: "15.22"
heading: "15.22. Bitwise and Logical Operators"
parent: "Chapter 15. Expressions"
source: https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.22
id: "15.22"
url: https://jvm.obfuscated.software/jls/se21/15.22.html
raw: https://jvm.obfuscated.software/jls/se21/15.22.md
---

# 15.22. Bitwise and Logical Operators

The *bitwise operators* and *logical operators* include the AND operator `&`, exclusive OR operator `^`, and inclusive OR operator `|`.

```
AndExpression:
  EqualityExpression
  AndExpression & EqualityExpression

ExclusiveOrExpression:
  AndExpression
  ExclusiveOrExpression ^ AndExpression

InclusiveOrExpression:
  ExclusiveOrExpression
  InclusiveOrExpression | ExclusiveOrExpression
```

These operators have different precedence, with `&` having the highest precedence and `|` the lowest precedence.

Each of these operators is syntactically left-associative (each groups left-to-right).

Each operator is commutative if the operand expressions have no side effects.

Each operator is associative.

The bitwise and logical operators may be used to compare two operands of numeric type or two operands of type `boolean`. All other cases result in a compile-time error.

## 15.22.1. Integer Bitwise Operators `&`, `^`, and `|`

When both operands of an operator `&`, `^`, or `|` are of a type that is convertible ([§5.1.8](5.1.8.md)) to a primitive integral type, binary numeric promotion is first performed on the operands ([§5.6](5.6.md)).

The type of the bitwise operator expression is the promoted type of the operands.

For `&`, the result value is the bitwise AND of the operand values.

For `^`, the result value is the bitwise exclusive OR of the operand values.

For `|`, the result value is the bitwise inclusive OR of the operand values.

For example, the result of the expression:

```
0xff00 & 0xf0f0
```

is:

```
0xf000
```

The result of the expression:

```
0xff00 ^ 0xf0f0
```

is:

```
0x0ff0
```

The result of the expression:

```
0xff00 | 0xf0f0
```

is:

```
0xfff0
```

## 15.22.2. Boolean Logical Operators `&`, `^`, and `|`

When both operands of a `&`, `^`, or `|` operator are of type `boolean` or `Boolean`, then the type of the bitwise operator expression is `boolean`. In all cases, the operands are subject to unboxing conversion ([§5.1.8](5.1.8.md)) as necessary.

For `&`, the result value is `true` if both operand values are `true`; otherwise, the result is `false`.

For `^`, the result value is `true` if the operand values are different; otherwise, the result is `false`.

For `|`, the result value is `false` if both operand values are `false`; otherwise, the result is `true`.
