Hitachi

Hitachi Advanced Database SQL Reference


8.11.5 BITRSHIFT

Returns the value resulting from shifting the bits of a binary data value to the right.

Organization of this subsection

(1) Specification format

scalar-function-BITRSHIFT ::= BITRSHIFT(target-data,number-of-bits-to-shift)
 
  target-data ::= value-expression
  number-of-bits-to-shift ::= value-expression

(2) Explanation of specification format

target-data:

Specifies the target binary data.

The following rules apply:

  • Specify the target data in the form of a value expression. For details about value expressions, see 7.20 Value expression.

  • Specify BINARY or VARBINARY type data for the target data.

  • You cannot specify a dynamic parameter by itself for the target data.

number-of-bits-to-shift:

Specifies the number of bits to shift the binary data.

The following rules apply:

  • Specify number-of-bits-to-shift in the form of a value expression. For details about value expressions, see 7.20 Value expression.

  • Specify an integer (data of type INTEGER or SMALLINT) for number-of-bits-to-shift.

  • If you specify a positive value for number-of-bits-to-shift, the return value is the target data shifted to the right.

  • If you specify a negative value for number-of-bits-to-shift, the return value is the target data shifted to the left.

  • If you specify 0 for number-of-bits-to-shift, the same binary data as the target data is returned.

  • If you specify a dynamic parameter by itself for number-of-bits-to-shift, the assumed data type of the dynamic parameter is INTEGER.

The following example illustrates the result of executing the scalar function BITRSHIFT.

Examples:

Return the specified binary data value with its bits shifted to the right.

BITRSHIFT (B'01011011',1)B'00101101'

BITRSHIFT (B'01011011',8)B'00000000'

BITRSHIFT (B'01011011',0)B'01011011'

BITRSHIFT (X'0F0F',8)X'000F'

If you specify a negative value for number-of-bits-to-shift, the return value is the target data shifted to the left.

BITRSHIFT (B'01011011',-3)B'11011000'

BITRSHIFT (X'0F0F',-16)X'0000'

(3) Rules

  1. The bits vacated by shifting are filled with B'0'.

  2. If the data length of the target data (or the actual length if the target data is VARBINARY type) is m, and the number of bits to shift is n, the execution result of BITRSHIFT will be as shown in the following table.

    Table 8‒41: Execution result of BITRSHIFT

    Conditions

    Execution result of BITRSHIFT

    n > 0

    8 × mn

    Returns binary data in which X'00' is set to the number of bytes in the data length of the target data (or actual length if the target data is VARBINARY type).

    0 < n < 8 × m

    Returns binary data in which the target data is shifted n bits to the right.

    n=0

    The same binary data as the target data is returned.

    n < 0

    0 < |n| < 8 × m

    Returns binary data in which the target data is shifted |n| bits to the left.

    Other than the above

    Returns binary data in which X'00' is set to the number of bytes in the data length of the target data (or actual length if the target data is VARBINARY type).

  3. The data type and data length of the execution result will be the data type and data length of the target data.

  4. The NOT NULL constraint does not apply to the value of the execution result (the null value is allowed).

  5. If either the target data or number-of-bits-to-shift is the null value, the execution result will be the null value.

  6. If the target data is binary data whose actual length is 0 bytes, the execution result will be binary data whose actual length is 0 bytes.