> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $bitXor (expression operator)

## Definition

**New in version 6.3**

Returns the result of a bitwise `xor` (exclusive or) operation on an array of `int` and `long` values.

## Syntax

The `$bitXor` operator has the following syntax:

```javascript
{ $bitXor: [ <expression1>, <expression2>, ... ] }
```

## Behavior

If the operands include both integers and long values, MongoDB sign-extends the calculated integer result and returns a long value. Otherwise, if the operands include only integers or only longs, MongoDB returns results with the corresponding value type.

**Note:**

All numbers in [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) are doubles, not integers. To specify integers in [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh), use the `Int32()` or the `Long()` constructor. To learn more, see [Int32](https://www.mongodb.com/docs/mongodb-shell/reference/data-types.md#std-label-shell-type-int) or [Long.](https://www.mongodb.com/docs/mongodb-shell/reference/data-types.md#std-label-shell-type-long)

To learn how your MongoDB driver handles numeric values, refer to your [driver's documentation.](https://www.mongodb.com/docs/drivers/)

If any arguments in the array are of a different data type such as a string, double, or decimal, MongoDB returns an error.

If the argument is an empty array, the operation returns `Int32(0)`.

If any of the arguments in the array equate to `null`, the operation returns `null`.

## Example

The example on this page uses the `switches` collection:

```javascript
db.switches.insertMany( [
    { _id: 0, a: Int32(0), b: Int32(127) },
    { _id: 1, a: Int32(2), b: Int32(3) },
    { _id: 2, a: Int32(3), b: Int32(5) }
] )
```

The following aggregation uses the `$bitXor` operator in the [`$project`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project) stage:

```javascript
db.switches.aggregate( [
  {
    $project: {
      result: {
        $bitXor: ["$a", "$b"]
      }
    }
  }
])
```

The operation returns the following results:

```javascript
  [
    { _id: 0, result: 127 },
    { _id: 1, result: 1 },
    { _id: 2, result: 6 }
  ]
```

## Learn More

- [Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-pipeline-operators)

- [$bit (update operator)](https://www.mongodb.com/docs/manual/reference/operator/update/bit.md#std-label-update-bit)
