Skip to content

Commit

Permalink
removed rightShift32 and fixed examples for other shift operations
Browse files Browse the repository at this point in the history
  • Loading branch information
nicc authored and Nic Young committed Dec 6, 2023
1 parent b63bfd2 commit f219032
Showing 1 changed file with 3 additions and 29 deletions.
32 changes: 3 additions & 29 deletions docs/zkapps/o1js/bitwise-operations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -238,39 +238,13 @@ Example:

```ts
const x = Provable.witness(Field, () => Field(0b001100)); // 12 in binary
const y = Gadgets.leftShift(x, 2); // left shift by 2 bits
const y = Gadgets.leftShift64(x, 2); // left shift by 2 bits
y.assertEquals(0b110000); // 48 in binary

const xLarge = Provable.witness(Field, () => Field(12345678901234567890123456789012345678n));
leftShift(xLarge, 32); // throws an error since input exceeds 64 bits
Gadgets.leftShift64(xLarge, 32); // throws an error since input exceeds 64 bits
``

## rightShift32()

```ts
rightShift32(field: Field, bits: number) => Field
```

The `rightShift32()` gadget is a provable method that supports the bitwise shifting operation that moves the bits of a binary number to the right. Unlike rotation, the bits that fall off at the end are discarded and the vacant positions are filled with zeros.

The operation:

- Performs a right shift operation on a Field element.
- Shifts bits to the right and discards the overflowing bits.
- Performs these operations with the big-endian 32-bit representation of the number, where the most significant (32nd) bit is on the left end and the least significant bit is on the right end.
- Requires values that are range checked to 32 bits. You can use [rangeCheck32](#rangecheck32).

Example:

```ts
const x = Provable.witness(Field, () => Field(0b001100)); // 12 in binary
const y = Gadgets.leftShift(x, 2); // left shift by 2 bits
y.assertEquals(0b110000); // 48 in binary
const xLarge = Provable.witness(Field, () => Field(12345678901234567890123456789012345678n));
leftShift(xLarge, 32); // throws an error since input exceeds 64 bits
```

## rightShift64()

```ts
Expand All @@ -293,7 +267,7 @@ const y = Gadgets.rightShift64(x, 2); // right shift by 2 bits
y.assertEquals(0b000011); // 3 in binary
const xLarge = Provable.witness(Field, () => Field(12345678901234567890123456789012345678n));
rightShift64(xLarge, 32); // throws an error since input exceeds 64 bits
Gadgets.rightShift64(xLarge, 32); // throws an error since input exceeds 64 bits
```

## rotate32()
Expand Down

0 comments on commit f219032

Please sign in to comment.