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

# $bitAnd (expression operator)

## Definition

**New in version 6.3**

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

## Syntax

The [`$bitAnd`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitand.md#mongodb-expression-exp.-bitAnd) operator has the following syntax:

```javascript
{ $bitAnd: [ <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(-1)`.

If any of the operands equate to `null`, the operation returns `null`.

## Examples

The examples on this page use the `switches` collection, which contains the following documents:

```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) }
] )
```

### Bitwise `AND` with Two Integers

The following aggregation uses the [`$bitAnd`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitand.md#mongodb-expression-exp.-bitAnd) 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: {
        $bitAnd: [ "$a", "$b" ]
      }
    }
  }
])
```

The operation returns the following results:

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

### Bitwise `AND` with a Long and Integer

The following aggregation uses the [`$bitAnd`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitand.md#mongodb-expression-exp.-bitAnd) 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: {
        $bitAnd: [ "$a", Long("63") ]
      }
    }
  }
])
```

The operation returns the following results:

```javascript
  [
    { _id: 0, result: Long("0") }
    { _id: 1, result: Long("2") }
    { _id: 2, result: Long("3") }
  ]
```

## 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)
