Working with Bits in Stream Analytics

Howes, Stephen 0 Reputation points
2023-06-26T18:53:46.3566667+00:00

I'm hoping someone can help. I have a Byte coming into Azure Stream Analytics. This has a label "Status1" and a value 128 at present, this is RAW data coming from Azure IOT Hub. I need to to split each individual bit out of this Byte and store it as a Boolean value, using the table below. Then a second column needs to display a sting depending on the Boolean state.

In SQL there is a GET_BIT command but the Query builder in Stream Analytics doesn't support this. Is there another command I could use?

enter image description here

Azure Stream Analytics
Azure Stream Analytics
An Azure real-time analytics service designed for mission-critical workloads.
335 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. QuantumCache 20,106 Reputation points
    2023-06-27T15:47:40.43+00:00

    Hello @Howes, Stephen,

    Here's an example query that demonstrates how to extract individual bits from a byte value and store them as Boolean values:

    WITH input AS (
        SELECT CAST('80' AS bigint) AS byteValue
    )
    
    SELECT
        byteValue,
        (byteValue & 128) > 0 AS bit7,
        (byteValue & 64) > 0 AS bit6,
        (byteValue & 32) > 0 AS bit5,
        (byteValue & 16) > 0 AS bit4,
        (byteValue & 8) > 0 AS bit3,
        (byteValue & 4) > 0 AS bit2,
        (byteValue & 2) > 0 AS bit1,
        (byteValue & 1) > 0 AS bit0
    INTO
        output
    FROM
        input
    
    
    

    Please let us know if you have further questions!

    0 comments No comments