NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

How to act on bitwise data

Solved!
Go to solution

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...)

 

0 Kudos
Message 1 of 6
(4,870 Views)

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.

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
Message 2 of 6
(4,857 Views)
Solution
Accepted by Everseeker

Other option:

(Locals.BurnerState AND 16) > 0

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
Message 3 of 6
(4,854 Views)

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.

0 Kudos
Message 4 of 6
(4,823 Views)

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?

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
0 Kudos
Message 5 of 6
(4,816 Views)

I am using them as individual flags, So I needed a way to detect any one of them in an IF statement

0 Kudos
Message 6 of 6
(4,810 Views)