Document toolboxDocument toolbox

Bitwise right shift (rshift, >>)

Description

Creates a new column that shifts to the right the bits of the values in the first argument as many positions as specified in the second argument. The result is then transformed and given in decimal format.

This operation uses an 8-bit register. The bits that are shifted out are discarded, and the bit positions that have been vacated by the shift operation are filled with the MSB bit of the value, which will be 0 if the number is positive, and 1 if it is negative. Therefore, the sign of the original number is always preserved.

The following example shows how the bits in the binary form of the value 23 (00010111) are shifted to the right 2 positions. The two last values (in red) are discarded, and the vacant positions are filled with zeros (in green) since the MSB bit of the value is 0. The result is 00000101, which corresponds to the decimal number 5.

You can also use the Bitwise unsigned right shift (urshift, >>>) operation, which doesn't preserve the sign of the original number and always fills the vacant positions with zeros. Keep in mind that for positive numbers, both the Bitwise right shift and Bitwise unsigned right shift operations will produce the same result, but they will return different outputs for negative numbers.

For more information about bitwise shifts, see this article.

How does it work in the search window?

Select Create column in the search window toolbar, then select the Bitwise right shift operation. You need to specify two arguments:

Argument

Data type

More information

Argument

Data type

More information

Number mandatory

integer 

Field whose values you want to transform.

Number mandatory

integer

Number of places to shift to the right.

The data type of the values in the new column is integer.


Example

The examples in this article use values in a data table generated from the following CSV file.

If you want to try the example for yourself, download the file and upload it to your domain clicking Data upload in the navigation pane. Name the new table my.upload.sample.data and select Current date as Date parsing type. Learn more about uploading data in Uploading log files.

After receiving the confirmation message, you can access the table from the Finder, selecting my → upload → sample → data. When you upload data from a file, all the information is included in a single column called message. To split the values and extract those we need for this operation into a new column, you can use the Split operation. Click Toggle Query Editor in the search window toolbar and paste the following LINQ query to save time:

from my.upload.sample.data
select split(message, ";", 17) as posNumbers1
select int(posNumbers1) as posNumbers1_int


We want to shift 2 positions to the right the bits of the values in the posNumbers1_int column, and we want to preserve the sign bit. To do it, we will create a new column using the Bitwise right shift operation. Let's call the new column rshift_2

The arguments needed to create the new column are:

  • Number - posNumbers1_int column

  • Number - Click the pencil icon and enter 2

Click Create column and you will see the following result:

How does it work in LINQ?

Use the operator select... as...  and add the operation syntax to create the new column. These are the valid formats of the Bitwise right shift operation:

  • rshift(integer1, integer2)

  • integer1 >> integer2

Example

You can copy the following LINQ scripts and try the above example on the my.upload.sample.data table. 

from my.upload.sample.data select split(message, ";", 17) as posNumbers1 select int(posNumbers1) as posNumbers1_int select rshift(posNumbers1_int, 2) as rshift_2

or

from my.upload.sample.data select split(message, ";", 17) as posNumbers1 select int(posNumbers1) as posNumbers1_int select posNumbers1_int >> 2 as rshift_2