| ... | ... | @@ -1244,8 +1244,9 @@ fn divide(a: i32, b: i32) i32 { |
| 1244 | 1244 | </p> |
| 1245 | 1245 | <p> |
| 1246 | 1246 | Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on |
| 1247 | | integer overflow. Also available are operations such as {#syntax#}+%{#endsyntax#} and |
| 1248 | | {#syntax#}-%{#endsyntax#} which are defined to have wrapping arithmetic on all targets. |
| 1247 | integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets. |
| 1248 | {#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic |
| 1249 | while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic. |
| 1249 | 1250 | </p> |
| 1250 | 1251 | <p> |
| 1251 | 1252 | Zig supports arbitrary bit-width integers, referenced by using |
| ... | ... | @@ -1395,6 +1396,24 @@ a +%= b{#endsyntax#}</pre></th> |
| 1395 | 1396 | <pre>{#syntax#}@as(u32, std.math.maxInt(u32)) +% 1 == 0{#endsyntax#}</pre> |
| 1396 | 1397 | </td> |
| 1397 | 1398 | </tr> |
| 1399 | <tr> |
| 1400 | <td><pre>{#syntax#}a +| b |
| 1401 | a +|= b{#endsyntax#}</pre></td> |
| 1402 | <td> |
| 1403 | <ul> |
| 1404 | <li>{#link|Integers#}</li> |
| 1405 | </ul> |
| 1406 | </td> |
| 1407 | <td>Saturating Addition. |
| 1408 | <ul> |
| 1409 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| 1410 | <li>See also {#link|@addWithSaturation#}.</li> |
| 1411 | </ul> |
| 1412 | </td> |
| 1413 | <td> |
| 1414 | <pre>{#syntax#}@as(u32, std.math.maxInt(u32)) +| 1 == @as(u32, std.math.maxInt(u32)){#endsyntax#}</pre> |
| 1415 | </td> |
| 1416 | </tr> |
| 1398 | 1417 | <tr> |
| 1399 | 1418 | <th scope="row"><pre>{#syntax#}a - b |
| 1400 | 1419 | a -= b{#endsyntax#}</pre></th> |
| ... | ... | @@ -1434,6 +1453,24 @@ a -%= b{#endsyntax#}</pre></th> |
| 1434 | 1453 | <pre>{#syntax#}@as(u32, 0) -% 1 == std.math.maxInt(u32){#endsyntax#}</pre> |
| 1435 | 1454 | </td> |
| 1436 | 1455 | </tr> |
| 1456 | <tr> |
| 1457 | <td><pre>{#syntax#}a -| b |
| 1458 | a -|= b{#endsyntax#}</pre></td> |
| 1459 | <td> |
| 1460 | <ul> |
| 1461 | <li>{#link|Integers#}</li> |
| 1462 | </ul> |
| 1463 | </td> |
| 1464 | <td>Saturating Subtraction. |
| 1465 | <ul> |
| 1466 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| 1467 | <li>See also {#link|@subWithSaturation#}.</li> |
| 1468 | </ul> |
| 1469 | </td> |
| 1470 | <td> |
| 1471 | <pre>{#syntax#}@as(u32, 0) -| 1 == 0{#endsyntax#}</pre> |
| 1472 | </td> |
| 1473 | </tr> |
| 1437 | 1474 | <tr> |
| 1438 | 1475 | <th scope="row"><pre>{#syntax#}-a{#endsyntax#}</pre></th> |
| 1439 | 1476 | <td> |
| ... | ... | @@ -1508,6 +1545,24 @@ a *%= b{#endsyntax#}</pre></th> |
| 1508 | 1545 | <pre>{#syntax#}@as(u8, 200) *% 2 == 144{#endsyntax#}</pre> |
| 1509 | 1546 | </td> |
| 1510 | 1547 | </tr> |
| 1548 | <tr> |
| 1549 | <td><pre>{#syntax#}a *| b |
| 1550 | a *|= b{#endsyntax#}</pre></td> |
| 1551 | <td> |
| 1552 | <ul> |
| 1553 | <li>{#link|Integers#}</li> |
| 1554 | </ul> |
| 1555 | </td> |
| 1556 | <td>Saturating Multiplication. |
| 1557 | <ul> |
| 1558 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| 1559 | <li>See also {#link|@mulWithSaturation#}.</li> |
| 1560 | </ul> |
| 1561 | </td> |
| 1562 | <td> |
| 1563 | <pre>{#syntax#}@as(u8, 200) *| 2 == 255{#endsyntax#}</pre> |
| 1564 | </td> |
| 1565 | </tr> |
| 1511 | 1566 | <tr> |
| 1512 | 1567 | <th scope="row"><pre>{#syntax#}a / b |
| 1513 | 1568 | a /= b{#endsyntax#}</pre></th> |
| ... | ... | @@ -1577,6 +1632,24 @@ a <<= b{#endsyntax#}</pre></th> |
| 1577 | 1632 | <pre>{#syntax#}1 << 8 == 256{#endsyntax#}</pre> |
| 1578 | 1633 | </td> |
| 1579 | 1634 | </tr> |
| 1635 | <tr> |
| 1636 | <td><pre>{#syntax#}a <<| b |
| 1637 | a <<|= b{#endsyntax#}</pre></td> |
| 1638 | <td> |
| 1639 | <ul> |
| 1640 | <li>{#link|Integers#}</li> |
| 1641 | </ul> |
| 1642 | </td> |
| 1643 | <td>Saturating Bit Shift Left. |
| 1644 | <ul> |
| 1645 | <li>See also {#link|@shlExact#}.</li> |
| 1646 | <li>See also {#link|@shlWithOverflow#}.</li> |
| 1647 | </ul> |
| 1648 | </td> |
| 1649 | <td> |
| 1650 | <pre>{#syntax#}@as(u8, 1) <<| 8 == 255{#endsyntax#}</pre> |
| 1651 | </td> |
| 1652 | </tr> |
| 1580 | 1653 | <tr> |
| 1581 | 1654 | <th scope="row"><pre>{#syntax#}a >> b |
| 1582 | 1655 | a >>= b{#endsyntax#}</pre></th> |
| ... | ... | @@ -1968,14 +2041,14 @@ const B = error{Two}; |
| 1968 | 2041 | a!b |
| 1969 | 2042 | x{} |
| 1970 | 2043 | !x -x -%x ~x &x ?x |
| 1971 | | * / % ** *% || |
| 1972 | | + - ++ +% -% |
| 1973 | | << >> |
| 2044 | * / % ** *% *| || |
| 2045 | + - ++ +% -% +| -| |
| 2046 | << >> <<| |
| 1974 | 2047 | & ^ | orelse catch |
| 1975 | 2048 | == != < > <= >= |
| 1976 | 2049 | and |
| 1977 | 2050 | or |
| 1978 | | = *= /= %= += -= <<= >>= &= ^= |={#endsyntax#}</pre> |
| 2051 | = *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |={#endsyntax#}</pre> |
| 1979 | 2052 | {#header_close#} |
| 1980 | 2053 | {#header_close#} |
| 1981 | 2054 | {#header_open|Arrays#} |
| ... | ... | @@ -11839,6 +11912,7 @@ AssignOp |
| 11839 | 11912 | / PLUSEQUAL |
| 11840 | 11913 | / MINUSEQUAL |
| 11841 | 11914 | / LARROW2EQUAL |
| 11915 | / LARROW2PIPEEQUAL |
| 11842 | 11916 | / RARROW2EQUAL |
| 11843 | 11917 | / AMPERSANDEQUAL |
| 11844 | 11918 | / CARETEQUAL |
| ... | ... | @@ -11873,6 +11947,8 @@ AdditionOp |
| 11873 | 11947 | / PLUS2 |
| 11874 | 11948 | / PLUSPERCENT |
| 11875 | 11949 | / MINUSPERCENT |
| 11950 | / PLUSPIPE |
| 11951 | / MINUSPIPE |
| 11876 | 11952 | |
| 11877 | 11953 | MultiplyOp |
| 11878 | 11954 | &lt;- PIPE2 |
| ... | ... | @@ -11881,6 +11957,7 @@ MultiplyOp |
| 11881 | 11957 | / PERCENT |
| 11882 | 11958 | / ASTERISK2 |
| 11883 | 11959 | / ASTERISKPERCENT |
| 11960 | / ASTERISKPIPE |
| 11884 | 11961 | |
| 11885 | 11962 | PrefixOp |
| 11886 | 11963 | &lt;- EXCLAMATIONMARK |
| ... | ... | @@ -12044,6 +12121,8 @@ ASTERISK2 &lt;- '**' skip |
| 12044 | 12121 | ASTERISKEQUAL &lt;- '*=' skip |
| 12045 | 12122 | ASTERISKPERCENT &lt;- '*%' ![=] skip |
| 12046 | 12123 | ASTERISKPERCENTEQUAL &lt;- '*%=' skip |
| 12124 | ASTERISKPIPE &lt;- '*|' ![=] skip |
| 12125 | ASTERISKPIPEEQUAL &lt;- '*|=' skip |
| 12047 | 12126 | CARET &lt;- '^' ![=] skip |
| 12048 | 12127 | CARETEQUAL &lt;- '^=' skip |
| 12049 | 12128 | COLON &lt;- ':' skip |
| ... | ... | @@ -12060,6 +12139,8 @@ EXCLAMATIONMARK &lt;- '!' ![=] skip |
| 12060 | 12139 | EXCLAMATIONMARKEQUAL &lt;- '!=' skip |
| 12061 | 12140 | LARROW &lt;- '&lt;' ![&lt;=] skip |
| 12062 | 12141 | LARROW2 &lt;- '&lt;&lt;' ![=] skip |
| 12142 | LARROW2PIPE &lt;- '&lt;&lt;|' ![=] skip |
| 12143 | LARROW2PIPEEQUAL &lt;- '&lt;&lt;|=' ![=] skip |
| 12063 | 12144 | LARROW2EQUAL &lt;- '&lt;&lt;=' skip |
| 12064 | 12145 | LARROWEQUAL &lt;- '&lt;=' skip |
| 12065 | 12146 | LBRACE &lt;- '{' skip |
| ... | ... | @@ -12069,6 +12150,8 @@ MINUS &lt;- '-' ![%=&gt;] skip |
| 12069 | 12150 | MINUSEQUAL &lt;- '-=' skip |
| 12070 | 12151 | MINUSPERCENT &lt;- '-%' ![=] skip |
| 12071 | 12152 | MINUSPERCENTEQUAL &lt;- '-%=' skip |
| 12153 | MINUSPIPE &lt;- '-|' ![=] skip |
| 12154 | MINUSPIPEEQUAL &lt;- '-|=' skip |
| 12072 | 12155 | MINUSRARROW &lt;- '-&gt;' skip |
| 12073 | 12156 | PERCENT &lt;- '%' ![=] skip |
| 12074 | 12157 | PERCENTEQUAL &lt;- '%=' skip |
| ... | ... | @@ -12080,6 +12163,8 @@ PLUS2 &lt;- '++' skip |
| 12080 | 12163 | PLUSEQUAL &lt;- '+=' skip |
| 12081 | 12164 | PLUSPERCENT &lt;- '+%' ![=] skip |
| 12082 | 12165 | PLUSPERCENTEQUAL &lt;- '+%=' skip |
| 12166 | PLUSPIPE &lt;- '+|' ![=] skip |
| 12167 | PLUSPIPEEQUAL &lt;- '+|=' skip |
| 12083 | 12168 | LETTERC &lt;- 'c' skip |
| 12084 | 12169 | QUESTIONMARK &lt;- '?' skip |
| 12085 | 12170 | RARROW &lt;- '&gt;' ![&gt;=] skip |