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
Copy file name to clipboardExpand all lines: website/docs/language/types.md
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,10 @@ Operators:
30
30
31
31
Note the lack of the `**` (exponentiation).
32
32
33
+
#### Arithmetic Shift
34
+
35
+
The shift operators `<<` and `>>` when applied to `int` are arithmetic shifts, which means that the value is multiplied or divided by `2^n` where `n` is the number of bits to shift. See [Bitshift and arithmetic shift](#bitshift-and-arithmetic-shift) for more details.
36
+
33
37
#### Number Formatting
34
38
35
39
Underscores can be used to separate the digits of a numeric literal to aid readability, e.g. `1_000_000`. Numbers can also be formatted in scientific notation, e.g. `1e6` or `1E6`. These can also be combined, e.g. `1_000e6`.
@@ -107,6 +111,10 @@ bytes noCapability = 0x;
107
111
bytes2 data = 0x12345678.slice(1, 3); // 0x3456
108
112
```
109
113
114
+
#### Bitshift
115
+
116
+
The shift operators `<<` and `>>` when applied to `bytes` are bitwise shifts, which means that the value is shifted by `n` bits, and the bits that are shifted off the end are dropped. So that means that the byte size of the result is the same as the byte size of the input. See [Bitshift and arithmetic shift](#bitshift-and-arithmetic-shift) for more details.
117
+
110
118
## Bytes types with semantic meaning
111
119
Some byte sequences hold specific meanings inside Bitcoin Cash contracts. These have been granted their own types, separate from the regular `bytes` type.
112
120
@@ -238,3 +246,30 @@ An overview of all supported operators and their precedence is included below. N
238
246
| 16 | Logical AND |`&&`|
239
247
| 17 | Logical OR |`\|\|`|
240
248
| 18 | Assignment |`=`|
249
+
250
+
### Bitshift and arithmetic shift
251
+
252
+
The shifting operators `<<` and `>>` are available for both `int` and `bytes` types, but they are different operations based on the type.
253
+
254
+
When applied to `int`, the operation is an arithmetic shift, which means that the value is multiplied or divided by `2^n` where `n` is the number of bits to shift.
255
+
256
+
When applied to `bytes`, the operation is a bitwise shift, which means that the value is shifted by `n` bits, and the bits that are shifted off the left end are dropped. So that means that the byte size of the result is the same as the byte size of the input.
257
+
:::caution
258
+
Be mindful what the type of the input is when using the shift operators, so you know which of these operations is being performed. On the same underlying value (but different types), arithmetic and bitwise shift will produce different results.
259
+
:::
260
+
261
+
**Arithmetic shift (`int`)** — the value is multiplied (`<<`) or divided (`>>`) by `2^n`:
**Bitwise shift (`bytes`)** — the bits move within a fixed-width buffer and any bits shifted past either end fall off. Note that the result keeps the same byte length as the input:
269
+
270
+
| Expression | Input bits | Result bits | Result |
0 commit comments