10-19-2016 02:36 PM
So, I have a number saved in Locals...
Call it Locals.BurnerState of type (number)
I want to do something if the umm... 4th(?) bit is set (16)
So, I Tried
If 16 AND Locals.BurnerState =1 then.... nope
If 16 AND Locals.BurnerState ==1 then.... nope
If 16 XOR Locals.BurnerState== Locals.BurnerState then.... nope
If 16^Locals.BurnerState== Locals.BurnerState then... nope
tried moving the 16 to the other side... still nope
The Expression Builder keeps telling me that the expression is not evaluating to a boolean
(Heck, It does not even like if Locals.BurnerState== Locals.BurnerState then)
1. what am I missing with respect to building the If/Then construct
2. Is there a better way to act on specific bits (Flags actually...)
Solved! Go to Solution.
10-19-2016 05:40 PM
So bitwise arithmetic takes the bits from one number and performs them agains the bits of another number.
For instance if I have 2 and 3 the bits look like 10 and 11 respectively.
So 2 AND 3 translates to 10 AND 11 = 10. 1 AND 1 = 1, 0 AND 1 = 0
So 2 AND 3 = 10 = 2 Your end value is a numeric.
Using my example if I want to check bit 2 (N) then I would do something like this:
2 >> (N-1) AND 1
2 >> (2-1) = 01
01 AND 1 = 1
1 = True
So your example should look like this:
((Locals.BurnerState >> (4 - 1)) AND 1) == 1
BTW ">>" is a shift operator which shifts the number by N bits.
10-19-2016 05:45 PM
Other option:
(Locals.BurnerState AND 16) > 0
10-20-2016 11:12 AM
Thanks.
Combining the info you provided, plus a little more from reading, I came up with
If ((Locals.BurnerState & 0x10) >> 4) == True
If ((Locals.BurnerState & 0x08) >> 3) == True
If ((Locals.BurnerState & 0x04) >> 2) == True
If ((Locals.BurnerState & 0x02) >> 1) == True
If (Locals.BurnerState & 0x01) == True
to test all 5 bits & act accordingly.
10-20-2016 11:40 AM
Why wouldn't you just do:
(Locals.BurnerState & 0b11111) == 0b11111
That will test them all at once and is a single line. Or are you just showing how you test them individually?
10-20-2016 11:51 AM
I am using them as individual flags, So I needed a way to detect any one of them in an IF statement