# Source: https://docs.oxla.com/sql-reference/sql-functions/math-functions/bitwise-shift-right.md

> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oxla.com/llms.txt
> Use this file to discover all available pages before exploring further.

# BITWISE SHIFT RIGHT

Bitwise shift operators in Oxla manipulate the bits of integer value by shifting them left or right.
These operations are fundamental in low-level data processing and optimization.

The bitwise **right shift (`>>`)** operator shifts the bits of an integer to the right by the specified number of positions.
For **integers**, this operation is equivalent to dividing the integer value by 2 raised to the power of the shift amount, discarding any remainder.
Unlike a logical shift, the right shift in Oxla is an **arithmetic** shift, meaning that the vacant leftmost bits are filled with the original sign bits
(the most significant bit) rather than zeros. This preserves the sign of the integer after the shift, ensuring correct behavior for signed values.
During the shift, low-order bits that move beyond the size limit are permanently lost.
However, the overall length of the bit string is preserved, with zeros padding on the left side to maintain the length.

## Syntax

The syntax for the BITWISE SHIFT RIGHT is as follows:

```sql  theme={null}
value >> shift_amount
```

## Parameters

* `value`: integer expression
* `shift_amount`: a **non-negative** integer specifying how many bit positions to shift

## Restrictions

Bitwise shift operators in Oxla require the shift amount to be a **non-negative** integer.
Oxla treats negative shift counts as valid by applying modulo arithmetic based on the bit width,
so shifting `1 >> -3` in a 32-bit integer is equivalent to shifting `1 >> 29`,
producing predictable results without errors or undefined behavior.

When performing bitwise right shift operations (>>) on 32-bit integer values in Oxla, the shift count is taken **modulo** 32,
just as with left shifts. This means:

* Shifting by a number of bits greater than or equal to 32 will wrap around
* For example `1 >> 35` is equivalent to `1 >> 3` because `35`$modulo$`32 = 3`

<Warning>
  If you shift by a value larger than or equal to 32, the actual shift will be the remainder after dividing by 32,
  which may lead to unexpected results if not carefully considered.
</Warning>

## Examples

For the needs of this section we will use a simplified version of the `film` table from the Pagila database, containing only the `title`, `rating` and `privilegs` columns. The complete schema for the `film` table can be found on the
<a href="https://www.postgresql.org/ftp/projects/pgFoundry/dbsamples/pagila/pagila/" target="_blank">Pagila</a> database website.

```sql  theme={null}
DROP TABLE IF EXISTS film;
CREATE TABLE film (
  title TEXT NOT NULL,
  rating TEXT,
  privileges INT NOT NULL
);
INSERT INTO film(title, rating, privileges) VALUES
  ('ATTRACTION NEWTON', 'PG-13', 1),     -- Free users
  ('CHRISTMAS MOONSHINE', 'NC-17', 2),   -- Premium users
  ('DANGEROUS UPTOWN', 'PG', 3),         -- Free + Premium users (bits 0 and 1)
  ('KILL BROTHERHOOD', 'G', 4),          -- Admin-only content
  ('HALLOWEEN NUTS', 'PG-13', 1),
  ('HOURS RAGE', 'NC-17', 2),
  ('PIANIST OUTFIELD', 'NC-17', 3),
  ('PICKUP DRIVING', 'G', 4),
  ('INDEPENDENCE HOTEL', 'NC-17', 1),
  ('PRIVATE DROP', 'PG', 2),
  ('SAINTS BRIDE', 'G', 3),
  ('FOREVER CANDIDATE', 'NC-17', 4),
  ('MILLION ACE', 'PG-13', 1),
  ('SLEEPY JAPANESE', 'PG', 2),
  ('WRATH MILE', 'NC-17', 3),
  ('YOUTH KICK', 'NC-17', 4),
  ('CLOCKWORK PARADISE', 'PG-13', 1);
```

<Note>
  * Privilege 1 (binary 0001): Free users can watch.
  * Privilege 2 (binary 0010): Premium users can watch.
  * Privilege 3 (binary 0011): Both free and premium users can watch.
  * Privilege 4 (binary 0100): Admin-only content.
</Note>

The query below uses the integer `right shift (>>)` operation, shifting the privileges value
right by 1 for the movie 'DANGEROUS UPTOWN':

```sql  theme={null}
UPDATE film
SET privileges = privileges >> 1
WHERE title = 'DANGEROUS UPTOWN';
```

After running the update, you can verify the change with:

```sql  theme={null}
SELECT title, privileges FROM film WHERE title = 'DANGEROUS UPTOWN';
```

Expected output:

```sql  theme={null}
       title       | privileges 
-------------------+------------
 DANGEROUS UPTOWN |          1
(1 row)
```