Skip to content

Commit cdeda6e

Browse files
committed
Update docs with more information about shitfing operator differences
1 parent 1bbda54 commit cdeda6e

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

website/docs/language/types.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Operators:
3030

3131
Note the lack of the `**` (exponentiation).
3232

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+
3337
#### Number Formatting
3438

3539
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;
107111
bytes2 data = 0x12345678.slice(1, 3); // 0x3456
108112
```
109113

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+
110118
## Bytes types with semantic meaning
111119
Some byte sequences hold specific meanings inside Bitcoin Cash contracts. These have been granted their own types, separate from the regular `bytes` type.
112120

@@ -238,3 +246,30 @@ An overview of all supported operators and their precedence is included below. N
238246
| 16 | Logical AND | `&&` |
239247
| 17 | Logical OR | `\|\|` |
240248
| 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`:
262+
263+
| Expression | Result | Equivalent value |
264+
| ---------- | ------ | ------------------------- |
265+
| `3 << 4` | `48` | `3 * 2^4` |
266+
| `100 >> 4` | `6` | `100 / 2^4` (rounds down) |
267+
268+
**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 |
271+
| ------------- | ------------------- | ------------------- | -------- |
272+
| `0x0102 << 1` | `00000001 00000010` | `00000010 00000100` | `0x0204` |
273+
| `0x0102 << 8` | `00000001 00000010` | `00000010 00000000` | `0x0200` |
274+
| `0x0102 >> 1` | `00000001 00000010` | `00000000 10000001` | `0x0081` |
275+
| `0x0102 >> 8` | `00000001 00000010` | `00000000 00000001` | `0x0001` |

0 commit comments

Comments
 (0)