Skip to content

Commit

Permalink
Direct users to bitwise operation platform differences (#5033)
Browse files Browse the repository at this point in the history
I didn't want to repeat too much of what was covered on the number
semantics page or focus too much on the platform differences, so I did
my best to find a minimal balance. Let me know what you think.

If we want to cover more of what @eernstg mentioned in
#5020 (comment),
such as potential workarounds, that would be best as a follow-up
expansion on the number semantics page rather than here.

**Staged:**
https://dart-dev--pr5033-fix-5020-spu2i1id.web.app/language/operators#bitwise-and-shift-operators

Closes #5020
  • Loading branch information
parlough authored Jul 14, 2023
1 parent f1feb87 commit 44b1130
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions examples/misc/test/language_tour/operators_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,18 @@ void main() {
assert((value & ~bitmask) == 0x20); // AND NOT
assert((value | bitmask) == 0x2f); // OR
assert((value ^ bitmask) == 0x2d); // XOR

assert((value << 4) == 0x220); // Shift left
assert((value >> 4) == 0x02); // Shift right

// Shift right example that results in different behavior on web
// due to negative operand:
assert((-value >> 4) == -0x03);

assert((value >>> 4) == 0x02); // Unsigned shift right
assert((-value >> 4) == -0x03); // Shift right
assert((-value >>> 4) > 0); // Unsigned shift right
// #enddocregion op-bitwise
});
}, testOn: 'vm');

test('if-null', () {
// #docregion if-null
Expand Down
15 changes: 14 additions & 1 deletion src/language/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ you’d use these bitwise and shift operators with integers.
| `>>>` | Unsigned shift right
{:.table .table-striped}

{{site.alert.note}}
The behavior of bitwise operations with large or negative operands
might differ between platforms.
To learn more, check out
[Bitwise operations platform differences][].
{{site.alert.end}}

Here’s an example of using bitwise and shift operators:

<?code-excerpt "misc/test/language_tour/operators_test.dart (op-bitwise)"?>
Expand All @@ -328,10 +335,15 @@ assert((value & bitmask) == 0x02); // AND
assert((value & ~bitmask) == 0x20); // AND NOT
assert((value | bitmask) == 0x2f); // OR
assert((value ^ bitmask) == 0x2d); // XOR
assert((value << 4) == 0x220); // Shift left
assert((value >> 4) == 0x02); // Shift right
// Shift right example that results in different behavior on web
// due to negative operand:
assert((-value >> 4) == -0x03);
assert((value >>> 4) == 0x02); // Unsigned shift right
assert((-value >> 4) == -0x03); // Shift right
assert((-value >>> 4) > 0); // Unsigned shift right
```

Expand All @@ -340,6 +352,7 @@ assert((-value >>> 4) > 0); // Unsigned shift right
requires a [language version][] of at least 2.14.
{{site.alert.end}}

[Bitwise operations platform differences]: /guides/language/numbers#bitwise-operations

## Conditional expressions

Expand Down

0 comments on commit 44b1130

Please sign in to comment.