SHR
Function Name |
SHR (Shift Right) |
---|---|
Description |
Moves each bit n positions to the right, losing any characters that are moved from the end of the string and replacing them with 0s at the start. So, if the string is shifted 2 positions to the right, the last two bits are removed and two extra 0s are added to the start of the string to replace them. |
Arguments |
IN {ANY_BIT} IN is a bit string. N {ANY_INT} N is an integer that defines the number of positions the SHR function will move the characters. For more information on the data types for the inputs and outputs, see Data Type Hierarchy. |
Returns |
Output {Same data type as input} The Output is the number that represents the new binary string. |
Example:
Function Block Diagram - SHR:
The SHR function has two inputs. The top input (IN) is 146 which corresponds to the binary string of 10010010. The bottom input (N) is 3 which instructs the function to shift the bits right by 3 positions. The output is 18 which corresponds to the binary string 10010.
So, the shift right function has deleted the last 3 bits and added 3 zeros to the start of the string:
Input String | 10010010 |
Shift Right x3 | 00010010 |
The leading zeros are ignored and so the binary string that results from 3 shifts left is 10010 which corresponds to the decimal value of 18. So the value of the output is 18.
ST Program - SHR:
The SHR function is used to shift the positions of the bits in the Input string to the right, losing any characters that are shifted from the end of the string and replacing them with zeros at the start of the string:
- Output := SHR (IN, N);
Where Output, IN and N are defined as variables earlier in the ST program.
Alternatively, you can use the following syntax:
- Output := SHR (IN:= 146, N:= 3);
Where the numbers are the input values (in this case, they are the same numbers as used in the Function Block Diagram example).