09-07-2015 06:47 AM
I recently came to know that all Boolean functions accept numeric values as well. But what is the purpose of it?
I found the Boolean functions outputting some random values while inputting numeric values..
Thank you!!
Best Regards,
Rashme SR
Solved! Go to Solution.
09-07-2015 06:54 AM
The definition of a bool is x != 0, so that's what the function should equate to.
/Y
09-07-2015 06:58 AM - edited 09-07-2015 06:58 AM
Hi Rashme,
But what is the purpose of it?
The purpose is to apply boolean functions to integer values.
Instead of scalar booleans you can put 8/16/32/64 bits in an integer value and you can apply a boolean function to the whole integer (and all it's bits) at once.
Quite handy once you learn such stuff (when programming µCs at the assembler level)!
09-07-2015 07:04 AM
Ah, i misunderstood. Gerd is spot on. If, e.g. you want to know if the 4th bit is set, you can AND with 8 and compare to 0. If 0 it wasn't set, else it was. Now how is that beneficial? Many protocols just bits as flags, e.g. Intermec uses a 32 bit integer to set all relays in a module and thus you need to filter and set bits with AND and OR to do it efficiently.
/Y
09-07-2015 07:06 AM
Another common use case is x AND 1 to see if a number is even/odd.
/Y
09-07-2015 07:13 AM
I dont understand clearly. Can you please attach an example?
09-07-2015 07:32 AM - edited 09-07-2015 07:33 AM
@Rashme_SR wrote:
I dont understand clearly. Can you please attach an example?
What do you not understand?
When boolean logic is used on integers, the logic is done on a per bit basis. I do this all the time for encoding and decoding data from specific protocols (some more wild than others). This is also needed for many data checks (like a CRC or Reed Solomon) and for encryption (AES, DES, 3DES).
09-07-2015 07:51 AM
Thank you so much.. finally i understood.. Doing binary calculations using numeric controls.. Its awesome
Regards,
Rashme SR