You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
* feat: Update of the Book→Operators page
* All precedences match Tact 1.3.0
* Added bitwise XOR operator `^`
* Fixed minor typos and corrected some descriptions
* Added a table of operators in decreasing order of their precedences
Note, that the only link leading to Language section now has a proper link to the Reference section.
This is intentional, as the Lang->Ref rename is almost there :)
* chore: reset alignment
* chore: corrected highlighting
* fix/feat: applied suggestions from a code review
* Property of division rounding down (division of real numbers without rounding them down won't have such property)
* Rounding towards -∞ mentioned and explained
* Bit masking example reference
Copy file name to clipboardExpand all lines: pages/book/message-mode.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ $+32$ | `SendDestroyIfZero{:tact}` | Current account must be destroyed
25
25
26
26
## Combining modes with flags
27
27
28
-
To make the [`Int{:tact}`][int] value for `mode` field of SendParameters, you just have to combine base modes with optional flags by applying the [`bitwise OR{:tact}`](/book/operators#binary-bitwise-or) operation.
28
+
To make the [`Int{:tact}`][int] value for `mode` field of `SendParameters{:tact}`, you just have to combine base modes with optional flags by applying the [`bitwise OR{:tact}`](/book/operators#binary-bitwise-or) operation.
29
29
30
30
For example, if you want to send a regular message and pay transfer fees separately, use the mode $0$ (default) and a flag $+1$ to get `mode` $= 1$, which is equal to using `SendPayGasSeparately{:tact}` constant.
@@ -8,10 +8,67 @@ This page lists all the operators in Tact in decreasing order of their [preceden
8
8
9
9
<Callout>
10
10
11
-
Note, that there are no implicit type conversions in Tact, so operators can't be used to, say, add values of different type or compare them in terms of equality without explicitly casting to the same type. That's done with certain functions from the standard library. See [`Int.toString(){:tact}`](/language/ref/strings#inttostring) for an example of such function.
11
+
Note, that there are no implicit type conversions in Tact, so operators can't be used to, say, add values of different type or compare them in terms of equality without explicitly casting to the same type. That's done with certain functions from the standard library. See [`Int.toString(){:tact}`](/ref/api-strings#inttostring) for an example of such function.
12
12
13
13
</Callout>
14
14
15
+
## Table of operators [#table]
16
+
17
+
The following table lists operators in order of decreasing [precedence](#precedence): from highest to lowest.
All operators on this page are given in order of decreasing precedence, from highest to lowest. Precedence is used to choose which operator would be considered in a particular situation. Whenever any ambiguity arises, Tact would prefer operators with higher precedence over those with lower.
@@ -27,7 +84,7 @@ Consider the following code:
27
84
28
85
Even though this example may be simple, neglection of precedence rules can often lead to confusing situations with operators. The correct order of operations can be ensured by wrapping every operation in [parentheses](#parentheses), since parentheses have the highest precedence of all expressions and operators there is.
29
86
30
-
## Parentheses, `()`
87
+
## Parentheses, `()`[#parentheses]
31
88
32
89
Parentheses (also can be called round brackets, `(){:tact}`) are more of a punctuation symbols than actual operators, but their [precedence](#precedence) is higher than precedence of any other operator. Use parentheses to override order of operations:
Binary slash (_division_) operator `/{:tact}` is used for integer division of two values, which truncates towards zero. An attempt to divide by zero would result in an error with [exit code 4](/book/exit-codes#4): `Integer overflow`.
167
+
Binary slash (_division_) operator `/{:tact}` is used for integer division of two values, which truncates towards zero if result is positive, and away from zero if result is negative. This is also called [rounding down](https://en.wikipedia.org/wiki/Rounding#Rounding_down) (or rounding towards $-∞$).
168
+
169
+
An attempt to divide by zero would result in an error with [exit code 4](/book/exit-codes#4): `Integer overflow`.
111
170
112
171
Can only be applied to values of type [`Int{:tact}`][int]:
113
172
114
173
```tact
115
174
let two: Int = 2;
116
175
two / 2; // 1
117
-
two / 1; // 2
176
+
two / 1; // 0
118
177
-1 / 5; // -1
178
+
-1 / -5; // 0
179
+
1 / -5; // -1
180
+
1 / 5; // 0
181
+
6 / 5; // 1, rounding down
182
+
-6 / 5; // -2, rounding down (towards -∞)
119
183
```
120
184
185
+
<Callout>
186
+
187
+
Note that the following relationship between the division and modulo operators always holds for `Int{:tact}` type:
188
+
189
+
```tact
190
+
a / b * b + a % b == a; // true for any Int values of `a` and `b`,
191
+
// except when `b` is equal to 0 and we divide `a` by 0,
192
+
// which is an attempt to divide by zero resulting in an error
193
+
```
194
+
195
+
</Callout>
196
+
121
197
#### Modulo, `%`[#binary-modulo]
122
198
123
199
Binary percent sign (_modulo_) operator `%{:tact}` is used for getting the modulo of an integer division, which must not be confused with getting a remainder. For two values of the same sign, modulo and remainder operations are equivalent, but when the operands are of different signs, the modulo result always has the same sign as the _divisor_ (value on the right), while the remainder has the same sign as the _dividend_ (value on the left), which can make them differ by one unit of the _divisor_.
@@ -127,15 +203,15 @@ Can only be applied to values of type [`Int{:tact}`][int]:
127
203
```tact
128
204
let two: Int = 2;
129
205
two % 2; // 0
130
-
1 % two; // 1
206
+
two % 1; // 1
131
207
132
208
1 % 5; // 1
133
209
-1 % 5; // 4
134
210
1 % -5; // -4
135
211
-1 % -5; // -1
136
212
```
137
213
138
-
The simplest way to avoid confusion between the two is to prefer using positive values via [`abs(x: Int){:tact}`](/language/ref/math#abs):
214
+
The simplest way to avoid confusion between the two is to prefer using positive values via [`abs(x: Int){:tact}`](/ref/api-math#abs):
Binary double greater than (_bitwise shift right_) operator `<<{:tact}` returns an integer which binary representation is the _left operand_ value shifted by the _right operand_ number of bits to the left. Excess bits shifted off to the left are discarded, and zero bits are shifted in from the right. This is a more effective way to multiply the _left operand_ by $2^n$, where $n$ is equal to the _right operand_. Going beyond the maximum value of an [`Int{:tact}`][int] will result in an error with [exit code 4](/book/exit-codes#4): `Integer overflow`.
292
+
Binary double greater than (_bitwise shift left_) operator `<<{:tact}` returns an integer which binary representation is the _left operand_ value shifted by the _right operand_ number of bits to the left. Excess bits shifted off to the left are discarded, and zero bits are shifted in from the right. This is a more effective way to multiply the _left operand_ by $2^n$, where $n$ is equal to the _right operand_. Going beyond the maximum value of an [`Int{:tact}`][int] will result in an error with [exit code 4](/book/exit-codes#4): `Integer overflow`.
217
293
218
294
Can only be applied to values of type [`Int{:tact}`][int]:
219
295
@@ -235,57 +311,51 @@ pow(2, 255) == 1 << 255; // true, but we're very close to overflow here!
235
311
236
312
</Callout>
237
313
238
-
#### Bitwise AND, `&`[#binary-bitwise-and]
314
+
###Relation [#binary-relation]
239
315
240
-
Binary ampersand (_bitwise and_) operator `&{:tact}` applies a [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND), which performs the [logical AND](#binary-logical-and) operation on each pair of the corresponding bits of operands. This is useful when we want to clear selected bits off a number, where each bit represents an individual flag or a boolean state, which makes it possible to "store" up to $257$ boolean values per integer, as all integers in Tact are $257$-bit signed.
316
+
Find bigger, smaller or equal values.
241
317
242
-
Can only be applied to values of type [`Int{:tact}`][int]:
318
+
#### Greater than, `>`[#binary-greater]
319
+
320
+
Binary _greater than_ operator `>{:tact}` returns `true{:tact}` if the left operand is greater than the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
243
321
244
322
```tact
245
323
let two: Int = 2;
246
-
two & 1; // 0
247
-
4 & 1; // 0
248
-
3 & 1; // 1
249
-
1 & 1; // 1
250
-
255 & 0b00001111; // 15
324
+
two > 2; // false
325
+
-1 > -3; // true
251
326
```
252
327
253
-
<Callout>
254
-
255
-
[Bitwise AND - Wikipedia](https://en.wikipedia.org/wiki/Bitwise_operation#AND)\
#### Greater than or equal to, `>=`[#binary-greater-equal]
257
329
258
-
</Callout>
330
+
Binary _greater than or equal to_ operator `>={:tact}` returns `true{:tact}` if the left operand is greater than or to the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
259
331
260
-
#### Bitwise OR, `|`[#binary-bitwise-or]
332
+
```tact
333
+
let two: Int = 2;
334
+
two >= 2; // true
335
+
-1 >= -3; // true
336
+
```
261
337
262
-
Binary bar (_bitwise or_) operator `|{:tact}` applies a [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR), which performs the [logical OR](#binary-logical-or) operation on each pair of the corresponding bits of operands. This is useful when we want to apply a specific [bitmask](https://en.wikipedia.org/wiki/Mask_(computing)).
338
+
#### Less than, `<`[#binary-less]
263
339
264
-
Can only be applied to values of type [`Int{:tact}`][int]:
340
+
Binary _less than_ operator `<{:tact}` returns `true{:tact}` if the left operand is less than the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
265
341
266
342
```tact
267
343
let two: Int = 2;
268
-
two | 1; // 3
269
-
4 | 1; // 5
270
-
3 | 1; // 3
271
-
1 | 1; // 1
272
-
273
-
255 | 0b00001111; // 255
274
-
0b11110000 | 0b00001111; // 255
344
+
two < 2; // false
345
+
-1 < -3; // false
275
346
```
276
347
277
-
<Callout>
278
-
279
-
[Bitwise OR - Wikipedia](https://en.wikipedia.org/wiki/Bitwise_operation#OR)\
#### Less than or equal to, `<=`[#binary-less-equal]
283
349
284
-
### Comparison [#binary-comparison]
350
+
Binary _less than or equal to_ operator `<={:tact}` returns `true{:tact}` if the left operand is less than or equal to the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
285
351
286
-
Perform inequality and equality checks, find bigger, smaller or equal values.
352
+
```tact
353
+
let two: Int = 2;
354
+
two <= 2; // true
355
+
-1 <= -3; // false
356
+
```
287
357
288
-
#### Equal and not equal, `==``!=`[#binary-equality]
358
+
###Equality and inequality, `==``!=`[#binary-equality]
289
359
290
360
Binary equality (_equal_) operator `=={:tact}` checks whether its two operands are _equal_, returning a result of type [`Bool{:tact}`][bool].
Binary _greater than_ operator `>{:tact}` returns `true{:tact}` if the left operand is greater than the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
422
+
Binary ampersand (_bitwise AND_) operator `&{:tact}` applies a [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND), which performs the [logical AND](#binary-logical-and) operation on each pair of the corresponding bits of operands. This is useful when we want to clear selected bits off a number, where each bit represents an individual flag or a boolean state, which makes it possible to "store" up to $257$ boolean values per integer, as all integers in Tact are $257$-bit signed.
423
+
424
+
Can only be applied to values of type [`Int{:tact}`][int]:
353
425
354
426
```tact
355
427
let two: Int = 2;
356
-
two > 2; // false
357
-
-1 > -3; // true
428
+
two & 1; // 0
429
+
4 & 1; // 0
430
+
3 & 1; // 1
431
+
1 & 1; // 1
432
+
433
+
255 & 0b00001111; // 15
434
+
0b11110000 & 0b00001111; // 15
358
435
```
359
436
360
-
#### Greater than or equal to, `>=`[#binary-greater-equal]
437
+
<Callout>
361
438
362
-
Binary _greater than or equal to_ operator `>={:tact}` returns `true{:tact}` if the left operand is greater than or to the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
439
+
[Bitwise AND - Wikipedia](https://en.wikipedia.org/wiki/Bitwise_operation#AND)\
Binary _less than_ operator `<{:tact}` returns `true{:tact}` if the left operand is less than the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
446
+
Binary caret (_bitwise XOR_) operator `^{:tact}` applies a [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), which performs the [logical exclusive OR](https://en.wikipedia.org/wiki/Exclusive_or) operation on each pair of the corresponding bits of operands. The result in each position is $1$ if only one of the bits is $1$, but will be $0$ if both are $0$ or both are $1$. In this it performs the comparison of two bits, giving $1$ if the two bits are different, and $0$ if they are the same.
447
+
448
+
It is useful for inverting selected bits of an operand (also called toggle or flip), as any bit may be toggled by "XORing" it with $1$.
449
+
450
+
Can only be applied to values of type [`Int{:tact}`][int]:
373
451
374
452
```tact
375
453
let two: Int = 2;
376
-
two < 2; // false
377
-
-1 < -3; // false
454
+
two ^ 3; // 1
455
+
4 ^ 1; // 0
456
+
3 ^ 1; // 3
457
+
1 ^ 1; // 0
458
+
459
+
255 ^ 0b00001111; // 240
460
+
0b11110000 ^ 0b00001111; // 240
378
461
```
379
462
380
-
#### Less than or equal to, `<=`[#binary-less-equal]
463
+
<Callout>
381
464
382
-
Binary _less than or equal to_ operator `<={:tact}` returns `true{:tact}` if the left operand is less than or equal to the right operand, and `false{:tact}` otherwise. Can only be applied to values of type [`Int{:tact}`][int]:
Binary bar (_bitwise OR_) operator `|{:tact}` applies a [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR), which performs the [logical OR](#binary-logical-or) operation on each pair of the corresponding bits of operands. This is useful when we want to apply a specific [bitmask](https://en.wikipedia.org/wiki/Mask_(computing)).
473
+
474
+
For example, _bitwise OR_ is commonly used in Tact to [combine base modes with optional flags](http://localhost:3000/book/message-mode#combining-modes-with-flags) by masking specific bits to $1$ in order to construct a target [message `mode`](/book/message-mode).
475
+
476
+
Can only be applied to values of type [`Int{:tact}`][int]:
383
477
384
478
```tact
385
479
let two: Int = 2;
386
-
two <= 2; // true
387
-
-1 <= -3; // false
480
+
two | 1; // 3
481
+
4 | 1; // 5
482
+
3 | 1; // 3
483
+
1 | 1; // 1
484
+
485
+
255 | 0b00001111; // 255
486
+
0b11110000 | 0b00001111; // 255
388
487
```
389
488
489
+
<Callout>
490
+
491
+
[Bitwise OR - Wikipedia](https://en.wikipedia.org/wiki/Bitwise_operation#OR)\
Binary logical AND ([logical conjunction](https://en.wikipedia.org/wiki/Logical_conjunction)) operator `&&{:tact}` returns `true{:tact}` if both operands are `true{:tact}`, and `false{:tact}` otherwise. It's short-circuited, meaning that it would immediately evaluate the whole expression as `false{:tact}` if the left operand is `false{:tact}`, without evaluating the right one.
@@ -395,7 +501,7 @@ Can only be applied to values of type [`Bool{:tact}`][bool]:
395
501
396
502
```tact
397
503
let iLikeTact: Bool = true;
398
-
iLikeTact && true; // true
504
+
iLikeTact && true; // true, evaluated both operands
399
505
iLikeTact && false; // false, evaluated both operands
0 commit comments