LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Logical shift negative numbers

So, I think i have found a bug in Labview.  I am using Labview 8.5

 

I have a very very simple Vi Where i want to Right shift a negative number.  For example:

 

-16 >> 2 = -4

 

labview bug 1.JPG labview bug 4.JPG

 

What i get is 16380.  If I mismatch the data types.  Meaning I right shift from a 16 integar and place the result into a 8 bit integar I get -4.  

 

labview bug 2.JPG labview bug 3.JPG

 

It would appear that the function is padding with leading zeros and not ones when handling negative numbers.   

 

Is this an issue that has been fixed already?  Or am i using the function incorrectly?

 

 

0 Kudos
Message 1 of 13
(8,325 Views)

Hi Gruntboy50,

 

LabView does what you told it to do!

Shift operations aren't "defined" for signed numbers. They are meant to be used with unsigned numbers only.

 

I changed your attachment to have some default data and, even more important,  to use binary display for the numbers. Now you can see that LV does the correct shifting, but as the number is signed you get an unexpected result. Smiley Wink

 

In the detailed help for "logical shift" it is said to shift in zeros ("...the output value is all zeros."). You better use the rotate with carry functions to shift in (binary) ones...

Message Edited by GerdW on 10-08-2009 10:23 PM
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 13
(8,313 Views)

The functionality you want is already available.

 

Simply use "Scale by power of 2" instead. It's a drop-in replacement 🙂

Message 3 of 13
(8,295 Views)

Hi Altenbach,

 

excellent suggestion (as always)... Much easier than to use carry bits.

 

But this thread is also a nice example of, hmm, not perfect context help: this function (logical shift) is explained using I16 numbers. Yes, it says zeros are shifted in. No, it doesn't say that signed integers aren't useful here...

 

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 13
(8,275 Views)

Your absolutely correct. 

 

Although everyone's suggestion is good for a work around.  The intent is to point out that this VI is broken.  The reason i was wanting to do a logical shift instead of multiplying/dividing by a power of 2 was to simulate the mathmatics closer to an Algorithm for an ASIC.  I want to see the same type of rounding errors the algorithm does.  

 

The Help files say that an integar value is a valid input.  If the function was only to work with positive numbers than unsigned inputs should have been defined in the help file.   Either way I feel that National Instruments needs to address something here.  I feel its easier to fix the VI.  

 

Unless my understanding of binary numbers is deeply flawed and then i apologize.  I am young....

 

 

 

0 Kudos
Message 5 of 13
(8,257 Views)
I think in the mean time I will do the rotate with carry as someone has already suggested. 
0 Kudos
Message 6 of 13
(8,252 Views)

Hi Gruntboy,

 

AFAIK "logical shifts" do always shift in zeros. So NI's implementation is correct. You can use signed numbers, but logical shift doesn't care about by design.

 

See this for an old CPU implementation... (ASL is the command for left shift)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 13
(8,249 Views)

GerdW,

 

 

Thanks that makes more sense.  I understand better. 

 

 

Is there a reason why it was done this way from the beginning?   BTW  your reasource was quite helpful.  

 

 

0 Kudos
Message 8 of 13
(8,244 Views)

Gruntboy50 wrote:

The reason i was wanting to do a logical shift instead of multiplying/dividing by a power of 2 was to simulate the mathmatics closer to an Algorithm for an ASIC.  I want to see the same type of rounding errors the algorithm does.  


A shift operates on bit patterns, so it will not retain the sign of signed integers. Any other behavior would be very bad.

 

The "scale by a power of 2" is identical to a shift operation for integers and as far as I know it uses bit shifting internally. A division by powers of two is a right-shift and a multiplication is a left shift. Unlike an arbitrary multiplication/division, this can (and is!) done in very easily in binary. It's slightly more complicated for floating point numbers, but the same ideas apply.

 

For signed integers, the "scale by a power of 2" retains the sign, which is the operation you apparently want. If you want to explicitly do it with bit shifting, you need to mask off the sign, shift, and then reapply the sign.

0 Kudos
Message 9 of 13
(8,209 Views)

Gruntboy,

     What I think you're looking for is an arithmetic shift function, while the function in Labview is a logical shift.  The difference is in the leading bit of the result when they shift a number to the right.  A logical shift right pads the numbers with '0's and an arithmetic shift right pads the numbers with their copies of their most significant bit.  This is very important when dealing with signed numbers, because the most significant bit (in standard two's complement notation) denotes whether or not a number is negative. 

 

     The number four (4) represented in six bits would be: 000100, and in two's complement negative four (-4) would be 111100.

 

A logical shift right, which pads the numbers with a '0' on the left would be:

000100 --> 000010  (4 --> 2)

111100 --> 011110  (-4 --> 30)

 

An arithmetic shift right, which pads the numbers with whatever their most significant bit is would be: 

000100 --> 000010  (4 --> 2)

111100 --> 111110  (-4 --> -2

 

I don't see  an arithmetic shift function in Labview, but you could build one by converting the number to an array of booleans and shifting to your heart's content.

 

When operating on signed numbers Arithmetic shift operations tend to be more useful, but I'm sure there would be quite a bit of confusion if the unsigned number 60 ("111110") gets right shifted and becomes 127! ("111111").

 

Finally, there is "one" (heh I'm so punny) special case for arithmetic shifting.  Consider how right shifting (logical and arithmetic) 8 results in 4, then 2, then 1, then 0.  What about arithmetic right shifting of -8?  The results would be -4, then -2, then -1...... then -1, then -1, then -1, etc. 

 

Worthwhile resources:

Two's compliment notation for negative binary numbers: http://en.wikipedia.org/wiki/Two's_complement

Logical shift explanation (w/ pictures!): http://en.wikipedia.org/wiki/Logical_shift

Arithmetic shift explanation (w/ pictures!): http://en.wikipedia.org/wiki/Arithmetic_shift

 

 

I hope this helps!

 

Hugs,

memoryleak 

 

P.S. I noticed that you mentioned you were modeling number operations of an ASIC.  This book (ISBN: 978-0534378042) was used in the Intro to Digital Logic class I took way too long ago, and I remember it hitting very hard on binary, signs and bit operations.  Looks like a used copy can be had on Amazon for less than $40 

Message Edited by memoryleak on 10-09-2009 11:00 AM
Message 10 of 13
(8,191 Views)