LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How exactly does the logical shift function work?

I think I am missing something very basic here - I am trying to left shift an 8 bit unsigned integer by one place (x<<1)in each iteration of a while loop. So if I start with a number, say 18, the first iteration I get 36, then 72, 144 and after this 32, 64 and so on. I'm not able to do this apparently simple operation in LabVIEW. I tried the logical shift function and a formula node, but both dont give me what I need. The formula node appears to be close in that it gives me 18,36,72,144 but after that I get 255 rather than 32. I dont understand what the logical shift function is doing here. I have attached a vi here that shows what I am trying to do. Any help explaining the mistake I am making will be greatly apprec
iated.

Thanks.
0 Kudos
Message 1 of 4
(5,829 Views)
You have the inputs confused. If you want to left-shift by 1 at each iteration, the "1" goes on the upper "y" terminal and the number from the shift register (the "shiftee") goes at the bottom. (see attached modification).
0 Kudos
Message 2 of 4
(5,829 Views)
Thanks. That explains the logical shift and I can do what I want to, but I am still curious about why the formula node does not work. Any clues about this?
0 Kudos
Message 3 of 4
(5,829 Views)
I virtually never use the formula node, but from a bit of casual testing with your VI it seems that the formula node records it as an overflow and pegs the output at 255 if bits are shifted out to the left. It almost seems like the calculation is done on more than 8 bits, then coerced instead of truncated for the 8 bit result.

If you want it to correctly work in your code, place e.g. a "ToU16" in from of the formula node and a "ToU8" right after it.

Strange... Maybe somebody can explain this behavior.

(similar overflow also occurs when left shifting U16 or U32 using the formula node)
0 Kudos
Message 4 of 4
(5,829 Views)