

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# $BitTand
<a name="bitAnd"></a>

Baru dari versi 8.0.1.

`$bitAnd`Operator di Amazon DocumentDB melakukan operasi AND bitwise pada nilai integer atau panjang.

**Parameter**
+ `expressions`: Array dua atau lebih ekspresi, yang dapat diselesaikan menjadi bilangan bulat atau panjang.

## Contoh (MongoDB Shell)
<a name="bitAnd-examples"></a>

Contoh berikut menunjukkan cara menggunakan `$bitAnd` operator untuk melakukan bitwise AND pada dua bidang.

**Buat dokumen sampel **

```
db.flags.insertMany([
  {_id: 1, a: 13, b: 10},
  {_id: 2, a: 7, b: 5},
  {_id: 3, a: 15, b: 9}
]);
```

**Contoh kueri **

```
db.flags.aggregate([
  { $project: { result: { $bitAnd: ["$a", "$b"] } } }
]);
```

**Keluaran **

```
[
  {_id: 1, result: 8},
  {_id: 2, result: 5},
  {_id: 3, result: 9}
]
```

Dalam biner: 13 (1101) DAN 10 (1010) = 8 (1000); 7 (0111) DAN 5 (0101) = 5 (0101); 15 (1111) DAN 9 (1001) = 9 (1001).

## Contoh kode
<a name="bitAnd-code"></a>

Untuk melihat contoh kode untuk menggunakan `$bitAnd` operator, pilih tab untuk bahasa yang ingin Anda gunakan:

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function example() {
  const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
  try {
    await client.connect();
    const db = client.db('test');
    const collection = db.collection('flags');
    const result = await collection.aggregate([
      { $project: { result: { $bitAnd: ["$a", "$b"] } } }
    ]).toArray();
    console.log(result);
  } finally {
    await client.close();
  }
}
example();
```

------
#### [ Python ]

```
from pymongo import MongoClient

def example():
    client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
    try:
        db = client['test']
        collection = db['flags']
        result = list(collection.aggregate([
            {'$project': {'result': {'$bitAnd': ['$a', '$b']}}}
        ]))
        print(result)
    finally:
        client.close()

example()
```

------