Skip to content

Commit 35a07a6

Browse files
authored
Merge pull request #2968 from abinav2307/bitwise-third-pass-validation
Bitwise third pass validation
2 parents 124aa23 + 05611fa commit 35a07a6

File tree

5 files changed

+117
-104
lines changed

5 files changed

+117
-104
lines changed

articles/cosmos-db/mongodb/vcore/operators/bitwise/$bitand.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.author: suvishod
77
ms.service: azure-cosmos-db
88
ms.subservice: mongodb-vcore
99
ms.topic: language-reference
10-
ms.date: 08/03/2025
10+
ms.date: 09/04/2025
1111
---
1212

1313
# $bitAnd
@@ -28,9 +28,9 @@ The `$bitAnd` operator performs a `bitwise AND` operation on integer values. It
2828
| --- | --- |
2929
| **`expression1, expression2, ...`** | Expressions that evaluate to integers. The `$bitAnd` operator performs a bitwise AND operation on all provided expressions. |
3030

31-
## Example
31+
## Examples
3232

33-
Let's understand the usage with sample json from `stores` dataset.
33+
Consider this sample document from the stores collection.
3434

3535
```json
3636
{
@@ -121,17 +121,18 @@ db.stores.aggregate([
121121
])
122122
```
123123

124-
The output helps derive a `combined flag` value from the two staff numbers, often used in permission or feature toggles.
125-
The bitwise AND of 19 (10011 in binary) and 20 (10100 in binary) equals 16 (10000 in binary).
124+
This query returns the following result.
126125

127126
```json
128-
{
129-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
130-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
131-
"fullTimeStaff": 19,
132-
"partTimeStaff": 20,
133-
"staffPermissionFlag": 16
134-
}
127+
[
128+
{
129+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
130+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
131+
"fullTimeStaff": 19,
132+
"partTimeStaff": 20,
133+
"staffPermissionFlag": 16
134+
}
135+
]
135136
```
136137

137138
### Example 2: Multiple value `$bitAnd`
@@ -156,15 +157,16 @@ db.stores.aggregate([
156157
])
157158
```
158159

159-
The query calculates a combined flag based on full-time and part-time staff counts, constrained to a maximum of 255.
160-
The operation performs bitwise AND on 19, 20, and 255, resulting in 16.
160+
This query returns the following result.
161161

162162
```json
163-
{
164-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
165-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
166-
"combinedFlag": 16
167-
}
163+
[
164+
{
165+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
166+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
167+
"combinedFlag": 16
168+
}
169+
]
168170
```
169171

170172
## Related content

articles/cosmos-db/mongodb/vcore/operators/bitwise/$bitnot.md

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ The `$bitNot` operator performs a bitwise NOT operation on integer values. It in
2828
| --- | --- |
2929
| **`expression`** | An expression that evaluates to an integer. The `$bitNot` operator performs a bitwise NOT operation on this value. |
3030

31-
## Example
31+
## Examples
3232

33-
Let's understand the usage with sample json from `stores` dataset.
33+
Consider this sample document from the stores collection.
3434

3535
```json
3636
{
@@ -91,7 +91,7 @@ Let's understand the usage with sample json from `stores` dataset.
9191

9292
### Example 1: Basic bitwise NOT operation
9393

94-
The example aggregation query performs a bitwise inversion on the staff count fields for a specific store document.
94+
The example aggregation query performs a bitwise inversion on the staff count fields for a specific store document. The inverted values can be used for special permission flags, feature toggles, or bitmask operations. The bitwise NOT of 14 results are -15, and the bitwise NOT of 8 results in -9. The observed result is due to two's complement representation where ~n = -(n+1).
9595

9696
```javascript
9797
db.stores.aggregate([
@@ -112,22 +112,24 @@ db.stores.aggregate([
112112
])
113113
```
114114

115-
The inverted values can be used for special permission flags, feature toggles, or bitmask operations. The bitwise NOT of 14 results are -15, and the bitwise NOT of 8 results in -9. The observed result is due to two's complement representation where ~n = -(n+1).
115+
This query returns the following result.
116116

117117
```json
118-
{
119-
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
120-
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
121-
"fullTimeStaff": 14,
122-
"partTimeStaff": 8,
123-
"invertedFullTime": -15,
124-
"invertedPartTime": -9
125-
}
118+
[
119+
{
120+
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
121+
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
122+
"fullTimeStaff": 14,
123+
"partTimeStaff": 8,
124+
"invertedFullTime": -15,
125+
"invertedPartTime": -9
126+
}
127+
]
126128
```
127129

128130
### Example 2: Using $bitNot with discount percentages
129131

130-
The example aggregation query extracts and processes discount information for a specific store and applies a bitwise NOT operation on each discount percentage.
132+
The example aggregation query extracts and processes discount information for a specific store and applies a bitwise NOT operation on each discount percentage. The bitwise NOT operation inverts all bits: 20 becomes -21 and 17 becomes -18.
131133

132134
```javascript
133135
db.stores.aggregate([
@@ -149,26 +151,27 @@ db.stores.aggregate([
149151
])
150152
```
151153

152-
Applies $bitNot to produce bitwise-inverted discount values for potential flag checks or encoded logic.
153-
The bitwise NOT operation inverts all bits: 20 becomes -21 and 17 becomes -18.
154+
This query returns the following results:
154155

155156
```json
156-
{
157-
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
158-
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
159-
"eventName": "Incredible Savings Showcase",
160-
"categoryName": "Microphone Stands",
161-
"discountPercentage": 17,
162-
"invertedDiscount": -18
163-
},
164-
{
165-
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
166-
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
167-
"eventName": "Incredible Savings Showcase",
168-
"categoryName": "Condenser Microphones",
169-
"discountPercentage": 20,
170-
"invertedDiscount": -21
171-
}
157+
[
158+
{
159+
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
160+
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
161+
"eventName": "Incredible Savings Showcase",
162+
"categoryName": "Microphone Stands",
163+
"discountPercentage": 17,
164+
"invertedDiscount": -18
165+
},
166+
{
167+
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
168+
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
169+
"eventName": "Incredible Savings Showcase",
170+
"categoryName": "Condenser Microphones",
171+
"discountPercentage": 20,
172+
"invertedDiscount": -21
173+
}
174+
]
172175
```
173176

174177
## Related content

articles/cosmos-db/mongodb/vcore/operators/bitwise/$bitor.md

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ The `$bitOr` operator performs a bitwise OR operation on integer values. It comp
2828
| --- | --- |
2929
| **`expression1, expression2, ...`** | Expressions that evaluate to integers. The `$bitOr` operator performs a bitwise OR operation on all provided expressions. |
3030

31-
## Example
31+
## Examples
3232

33-
Let's understand the usage with sample json from `stores` dataset.
33+
Consider this sample document from the stores collection.
3434

3535
```json
3636
{
@@ -91,7 +91,7 @@ Let's understand the usage with sample json from `stores` dataset.
9191

9292
### Example 1: Basic bitwise OR operation
9393

94-
The query performs a bitwise OR operation on the staff values of a specific store document to combine permission flags.
94+
The query performs a bitwise OR operation on the staff values of a specific store document to combine permission flags. The bitwise OR of 3 (011 in binary) and 2 (010 in binary) equals 3 (011 in binary).
9595

9696
```javascript
9797
db.stores.aggregate([
@@ -109,21 +109,23 @@ db.stores.aggregate([
109109
])
110110
```
111111

112-
The bitwise OR of 3 (011 in binary) and 2 (010 in binary) equals 3 (011 in binary).
112+
This query returns the following result.
113113

114114
```json
115-
{
116-
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
117-
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
118-
"fullTimeStaff": 3,
119-
"partTimeStaff": 2,
120-
"combinedStaffFlag": 3
121-
}
115+
[
116+
{
117+
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
118+
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
119+
"fullTimeStaff": 3,
120+
"partTimeStaff": 2,
121+
"combinedStaffFlag": 3
122+
}
123+
]
122124
```
123125

124126
### Example 2: Multiple value bitwise OR with discount percentages
125127

126-
The example aggregation query extracts discount details for a specific promotion event and computes a bitwise flag combining discounts and staff values
128+
The example aggregation query extracts discount details for a specific promotion event and computes a bitwise flag combining discounts and staff values. The output shows the results of the aggregation query that calculates a combined bitwise flag for each discount in the event `Super Saver Spectacular`. The operation combines discount percentages with staff numbers using bitwise OR: 7|3|2 = 7 and 11|3|2 = 11.
127129

128130
```javascript
129131
db.stores.aggregate([
@@ -156,26 +158,28 @@ db.stores.aggregate([
156158
])
157159
```
158160

159-
The output shows the results of the aggregation query that calculates a combined bitwise flag for each discount in the event `Super Saver Spectacular`. The operation combines discount percentages with staff numbers using bitwise OR: 7|3|2 = 7 and 11|3|2 = 11.
161+
This query returns the following result.
160162

161163
```json
162-
{
163-
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
164-
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
165-
"eventName": "Super Saver Spectacular",
166-
"discountFlags": [
167-
{
168-
"categoryName": "Car Chargers",
169-
"discountPercentage": 7,
170-
"combinedFlag": 7
171-
},
172-
{
173-
"categoryName": "Dash Cameras",
174-
"discountPercentage": 11,
175-
"combinedFlag": 11
176-
}
177-
]
178-
}
164+
[
165+
{
166+
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
167+
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
168+
"eventName": "Super Saver Spectacular",
169+
"discountFlags": [
170+
{
171+
"categoryName": "Car Chargers",
172+
"discountPercentage": 7,
173+
"combinedFlag": 7
174+
},
175+
{
176+
"categoryName": "Dash Cameras",
177+
"discountPercentage": 11,
178+
"combinedFlag": 11
179+
}
180+
]
181+
}
182+
]
179183
```
180184

181185
## Related content

articles/cosmos-db/mongodb/vcore/operators/bitwise/$bitxor.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ The `$bitXor` operator performs a bitwise exclusive OR (XOR) operation on intege
2828
| --- | --- |
2929
| **`expression1, expression2, ...`** | Expressions that resolve to integer values. The operator performs XOR operations on these values in sequence. |
3030

31-
## Example
31+
## Examples
3232

33-
Let's understand the usage with sample json from `stores` dataset.
33+
Consider this sample document from the stores collection.
3434

3535
```json
3636
{
@@ -103,7 +103,7 @@ Let's understand the usage with sample json from `stores` dataset.
103103

104104
### Example 1: Basic XOR operation
105105

106-
The query uses an aggregation pipeline to calculate between full-time and part-time staff counts for a specific store.
106+
This query uses an aggregation pipeline to calculate between full-time and part-time staff counts for a specific store. The resulting document contains the store details along with a computed field. The XOR operation between 19 (binary: 10011) and 20 (binary: 10100) results in 7 (binary: 00111).
107107

108108
```javascript
109109
db.stores.aggregate([
@@ -121,21 +121,23 @@ db.stores.aggregate([
121121
])
122122
```
123123

124-
The output document contains the store details along with a computed field. The XOR operation between 19 (binary: 10011) and 20 (binary: 10100) results in 7 (binary: 00111).
124+
This query returns the following result.
125125

126126
```json
127-
{
128-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
129-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
130-
"fullTimeStaff": 19,
131-
"partTimeStaff": 20,
132-
"staffXor": 7
133-
}
127+
[
128+
{
129+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
130+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
131+
"fullTimeStaff": 19,
132+
"partTimeStaff": 20,
133+
"staffXor": 7
134+
}
135+
]
134136
```
135137

136138
### Example 2: XOR with Multiple Values
137139

138-
The aggregation pipeline computes the bitwise XOR of all discount percentages for the `Discount Delight Days` event of a specific store.
140+
The aggregation pipeline computes the bitwise XOR of all discount percentages for the `Discount Delight Days` event of a specific store. The resulting document represents the bitwise XOR calculation of all discount percentages for the `Discount Delight Days` event.
139141

140142
```javascript
141143
db.stores.aggregate([
@@ -174,16 +176,18 @@ db.stores.aggregate([
174176
])
175177
```
176178

177-
The output represents the bitwise XOR calculation of all discount percentages for the `Discount Delight Days` event.
179+
This query returns the following result.
178180

179181
```json
180-
{
181-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
182-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
183-
"eventName": "Discount Delight Days",
184-
"discountPercentages": [22, 23, 10, 10, 9, 24],
185-
"xorResult": { "$numberLong": "16" }
186-
}
182+
[
183+
{
184+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
185+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
186+
"eventName": "Discount Delight Days",
187+
"discountPercentages": [22, 23, 10, 10, 9, 24],
188+
"xorResult": { "$numberLong": "16" }
189+
}
190+
]
187191
```
188192

189193
## Related content
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
- name: $bitand
1+
- name: $bitAnd
22
href: $bitand.md
3-
- name: $bitnot
3+
- name: $bitNot
44
href: $bitnot.md
5-
- name: $bitor
5+
- name: $bitOr
66
href: $bitor.md
7-
- name: $bitxor
7+
- name: $bitXor
88
href: $bitxor.md

0 commit comments

Comments
 (0)