Boolean operations on strings

Jeff_wu
Jeff_wu Altair Community Member
edited April 2023 in Community Q&A

I am using Real Time Serial Read to read serial communication from an Arduino. I want to perform boolean operations on the received string for further processing, but it seems that Embed does not support direct boolean operations on strings. What should I do? Do I need to convert the string to binary first, or is there a faster way? If I need to convert the string to binary, how can I do that? Alternatively, are there any other better ways to perform boolean operations on strings?

Thank you for your response. It will be helpful to me.

Answers

  • pdarnell
    pdarnell
    Altair Employee
    edited April 2023

    You can use Boolean AND,OR,XOR directly with numeric strings. They will be converted to integers, then operated on in a bitwise manner. So this will work fine:

    image

    note that while single digit ones and zeros work as you would expect, 0x2 AND 0x1 = 0, since it is a bitwise operator. So 0x3 AND 0x1 =1

    If you want to treat your numeric strings as zero vs non zero do this:

    image

  • Jeff_wu
    Jeff_wu Altair Community Member
    edited April 2023

    Can I perform bitwise operations directly on a string? For example, whether the received string is equal to "Hello", it may receive more than two different strings.

Welcome!

It looks like you're new here. Sign in or register to get started.

Welcome!

It looks like you're new here. Sign in or register to get started.