LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Interrogate hexadecimal number for single bit

I have a hexadecimal number, ranging from 0-FFFF. When converted to binary, (0010010000001000) this represents the status of sixteen switches; switch no. 0 is the rightmost bit, and switch no. 15 is the leftmost.

I realise there must be lots of ways, but whats the *easiest* way to get the status of one switch (eg find out if switch 3 is on, given the status string 12B4)?

Many Thanks

Joolz
0 Kudos
Message 1 of 6
(3,380 Views)
For a 16 bit number, you could. split it into two 8 bit numbers, and convert those to arrays of booleans. Then, you could rebuild the arrays into one, and know your bit value.

Preferably (as it uses far less memory and processing time), you could use bitwise logic, and OR (or is it AND) the data with a mask. The mask would be everything but your bit of interest as a 1 (or is it 0) combined with your number to get ONLY the results of the bit in question. Upon some experimentation, it seems that if you NOT the binary number in question, and AND it with all 0s but the bit you are looking for, then use the logical shift negative the number of the bit you are looking for -1, that you will get your answer.

So, these are the steps:
*NOT your data
*AND it
with all 0's except the bit in question
*Logical Shift negative (-) the number corresponding to the bit number minus one, i.e. -3 for the fourth bit.


If you have any trouble, please post here again.
Message 2 of 6
(3,380 Views)
Another option is just to AND your data with the bit value of your switch, and check the result for zero. If false, your input bit is a 1. If TRUE, your input bit is a 0
Message 3 of 6
(3,380 Views)
Joolz wrote:
> I have a hexadecimal number, ranging from 0-FFFF. When converted to
> binary, (0010010000001000) this represents the status of sixteen
> switches; switch no. 0 is the rightmost bit, and switch no. 15 is the
> leftmost.
>
> I realise there must be lots of ways, but whats the *easiest* way to
> get the status of one switch (eg find out if switch 3 is on, given the
> status string 12B4)?
>
> Many Thanks
>
> Joolz
I use "Number to Boolen Array" -> "Index array"
0 Kudos
Message 4 of 6
(3,380 Views)
OK, thanks to you all - the AND method appealed to me most - I've attached what I ended up doing for other forum users who might have the same question. If you're pre version 6 then you can copy the vi from the screenshot.
Joolz
Download All
0 Kudos
Message 5 of 6
(3,380 Views)
Oops! the vi's I posted don't work for switches 8-15, because the Labview function "Scale by power 2" returns zero if you put a value of 8 or more into "n" (Don't ask me why, I didn't write it!)

Instead, use an expression node, with the expression "2**x" for LV6, or "2^x" for LV5 or previous. I'll repost the VIs when I get a minute

J
0 Kudos
Message 6 of 6
(3,380 Views)