web
You’re offline. This is a read only version of the page.
close
Support Portal

Understanding arithmetic shifts and bitwise operations

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.

  • Process Data: 2 Byte  = Distance (14 Bit) + Q1 + Q2
  • byte [0] = 31
  • byte [1] = 65


First we can extract the information from Q1 and Q2 (in the second byte) via a bitmask:

  • Q1 = byte [1] & 00000010
    • Q1 = 0
  • Q2 = byte [1] & 00000001
    • Q2 = 1

Then we can get the distance information through bit shifts:

  • Distance = (byte [0] << 6) + (byte [1] >> 2)
    • Distance = 2000

Alternatively, first add the 2 bytes together to get the full 16 bit process data and then shift out the last two bits:

  • Distance = ((byte [0] << 8) + byte [1]) >> 2

 

Byte Decimal Distance (14 bit) Q1 Q2
    16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
byte [0] = 31 31                 0 0 0 1 1 1 1 1
byte [1] = 65 65                 0 1 0 0 0 0 0 1
Bitmask

Q1 = 0 -> byte [1] & 00000010

Q2 = 1 -> byte [1] & 00000001

    16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
byte [0] << 6       0 0 0 1 1 1 1 1 << 6
byte [1] >> 2                   >> 2 0 1 0 0 0 0
byte [0] + byte [1] 2000     0 0 0 1 1 1 1 1 0 1 0 0 0 0

 

If 4 bytes have to be combined, they must be shifted further depending on the order e.g:

  • pd = (byte [0] << 24) + (byte [1] << 16) + (byte [2] << 8) + byte [3]

Or with the multiplication 2^n (2^24 = 16.777.216; 2^16 = 65.536; 2^8 = 256):

  • pd = (byte [0] * 16.777.216) + (byte [1] * 65.536) + (byte [2] * 256) + byte [3]

 

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