BITWISE SHIFT LEFT
Overview
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 left shift (<<
) operator shifts the bits of an integer to the left by the specified shift amount.
For integers, this operation is equivalent to multiplying the integer value by 2 raised to the power of the shift amount.
During this operation, high-order bits that are shifted out are permanently lost without the ability to be preserved,
while zeros are shifted in from the right to fill the vacant positions.
Because the left shifts operation (<<) on signed integers is arithmetic, meaning it shifts all bits to the left and fills the vacant rightmost bits with zeros on the right,
the behavior is the same as a logical shift in this case.
However, the overall length of the bit string is preserved, with zeros padding on the right to maintain the length.
Syntax
The syntax for the BITWISE SHIFT LEFT is as follows:
Parameters
value
: integer expressionshift_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 left shift operations (<<) on 32-bit integer values in Oxla, the shift count is taken modulo 32. This means:
- Shifting by a number of bits greater than or equal to 32 will wrap around
- For example,
1 << 35
is equivalent to1 << 3
because35
32 = 3
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.
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
Pagila database website.
- 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.
The query below uses the integer Left shift (<<)
operation, shifting the privileges value
left by 1 for the movie ‘ATTRACTION NEWTON’:
After running the update, you can verify the change with:
Expected output: