Your SICK ID gives you access to our extensive range of services. This includes direct online orders, price and availability checks, and access to our digital services.
Understanding arithmetic shifts and bitwise operations
Article No: KA-09100
Version: 1.0
Subject to change without notice
Table of Contents
The process data from IO-Link sensors often has to be calculated and split up for further processing of the information (especially if the IO-Link master does not provide an IODD interpretation on board - e.g. when using the SIG350 via the REST API). To do this, it is necessary to know the most common operations.
Bitwise Left Shift (<<): The bitwise left shift operator (<<) shifts the bits of a binary number to the left by a specified number of positions. The vacant positions on the right are filled with zeros. Each left shift effectively multiplies the number by 2, so each shift by n positions is equivalent to multiplication by 2^n.
Example:
Byte
Decimal
Bit
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
byte
7
0
0
0
0
0
1
1
1
byte << 8 or byte * (2^8)
1792
0
0
0
0
0
1
1
1
0
0
0
0
0
0
0
0
Bitwise Right Shift (>>): The bitwise right shift operator (>>) shifts the bits of a binary number to the right by a specified number of positions. The vacant positions on the left are filled based on the sign bit (0 for positive numbers, 1 for negative numbers in two's complement representation). Each right shift effectively divides the number by 2, so each shift by n positions is equivalent to division by 2^n.
Example:
Byte
Decimal
Bit
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
byte
65
0
1
0
0
0
0
0
1
byte >> 2 or byte / (2^2)
16
0
0
0
1
0
0
0
0
Note: This is true for positive integers. When using bit shifts and multiplication for negative numbers or floating-point numbers, additional considerations may be necessary, especially due to rounding behavior and the representation of negative numbers in two's complement.
Bitmask and logical AND (&): A bitmask is a sequence of bits that are used to perform bitwise operations. It is often used to extract, set or clear specific bits in a binary number.
Example - extract the last bit from the original 8-bit binary number:
Byte
Decimal
Bit
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
byte
65
0
1
0
0
0
0
0
1
byte & 00000001
1
0
0
0
0
0
0
0
1
Example with real data: Please check and consider the correct bit/ byte order of your used sensor!
We receive 2 bytes of process data from the sensor. The first 14 bits correspond to a distance value and the last 2 bits to the switching outputs.
The operation may look slightly different depending on the programming language or application, but the principle remains the same. For example, in Excel, "=BITRSHIFT()" is used for a bitwise right shift (>>) and "=BITLSHIFT()" for a bitwise left shift (<<).
In summary
Bitwise left shift and right shift are operations that move the bits of a binary number to the left or right, respectively. Bitmasks are used in conjunction with bitwise operations to manipulate or extract specific bits within a binary number.
Keywords: left shift, right shift, shift, bitmask, logical and, bitwise operations, arithmetic shifts