Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift operators in PL/SQL

Whether there is an alternative of shift operators in PL/SQL? There is bitand function, but it accepts only binary_integer-type arguments.

What should I do if I need check up lower/higher bit of really long number (probably set in the line)?

In C there are << and >> operators. How I can realise them in PL/SQL?

like image 919
drnk Avatar asked Apr 22 '09 09:04

drnk


People also ask

Which are the shift operators?

The bitwise shift operators are the right-shift operator ( >> ), which moves the bits of an integer or enumeration type expression to the right, and the left-shift operator ( << ), which moves the bits to the left.

What is << in PL SQL?

The <> syntax is for naming loops. Useful when you have nested loops and need to use the EXIT loop_name WHEN ... syntax to control which loop to exit.

What is the use of << shift operator?

The left shift operator ( << ) shifts the first operand the specified number of bits, modulo 32, to the left. Excess bits shifted off to the left are discarded.


1 Answers

The following answer is not endianness agnostic and my wording is based on little endian format...

You can shift bits simply multiplying (shift left) or dividing (shift right) the argument by 2 to the power of x where x is the number of bits to shift. for example, if I need to shift the low-order byte of a number (255:11111111) 16 bits to the left I would perform the following operation:

select 255 * power(2,16) from dual;  
-- the result will be (16711680:111111110000000000000000)

conversely, if I want to shift the value 16711680 16 bits to the right I would perform the following:

select 16711680 / power(2,16) from dual;
-- the result will be (255:11111111)
like image 115
Tim Lowes Avatar answered Oct 24 '22 11:10

Tim Lowes