ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Error in bit manipulation

This seems like a math script error, but maybe I am missing something: The bit manipulation functions seem to handle 0 incorrectly:
>>bitshift(0,1)
ans =

           2    
I expected 0.
>>bitget(0, 1:3)
ans =

           1      0      0    
I expected 0 0 0.

Is there a fix for this or something I am missing?

Thanks
0 Kudos
Message 1 of 5
(7,700 Views)
What is your LaVIEW version?
 
I just tested in 8.5 and the bitget(0, 1:3) now correctly returns 0 0 0.
 
The bitshift function is still incorrect when the first argument is zero.
0 Kudos
Message 2 of 5
(7,692 Views)
Oops sorry forgot to list that ... I am running LabView 8.2
0 Kudos
Message 3 of 5
(7,690 Views)

DrLock, altenbach,

The help file entry for bitshift does specify that the first argument should be a "positive integer" -- making the output for any other input undefined.  However I agree with you both that this is not the way it should behave and have sent it to the developers for them to look at.  This was reported to R&D (ID# 4ELI7NVQ) for further investigation.

One possible workaround would be to simply check for a zero input with a snippet of code such as:

if a==0
    ans=0
else
    ans=bitshift(a,1)
end

I hope this helps.

Regards,

Simon H
Applications Engineer
National Instruments
http://www.ni.com/support/



from the LabVIEW Help

d = bitshift(a, b, c)

Description

Performs a bitwise shift on the input elements. For example, if a = 9 (1001), bitshift(a, 1) = 18 (10010) and bitshift(a, 1, 4) = 2 (0010).

Examples

Inputs

Name Description
a Specifies a scalar, vector, or matrix of positive integers. All elements of a must be less than bitmax.
b Determines the shift sizes. Shifting a by b bits is equivalent to multiplying a by 2^b and then rounding to the nearest integer. b is a scalar, vector, or matrix of integers. b is the same size as a unless a or b is a scalar.
c Specifies the number of valid bits for each of the shifted integers. If the shifted integers exceed c bits, LabVIEW ignores the overflow. c is a matrix of integers. The default is 53.




0 Kudos
Message 4 of 5
(7,681 Views)

Hmm... so the labview help is more restrictive here for no good reason.

I was consulting the matlab help, which is more general:

"C = bitshift(A, k) returns the value of A shifted by k bits. Input argument A must be an unsigned integer or an array of unsigned integers."

Clearly, the function should be defined the same in both environments. 🙂

0 Kudos
Message 5 of 5
(7,678 Views)