12-13-2007 11:31 AM
45703 -> 1011000000010001. bits 7-9 from the right are 000 which equals 0
To expand the problem:
45806 -> 1011001011101110. Bits 13-14 from the right are 10 which is 2
45703 -> 1011000000010001. Bits 13-14 from the right are 10 which is 2
I have a row-by-row subroutine with for loops that accomplishes this, but I am hoping there is a way to make it faster by using a FormulaCalc or ChnCalculate command and getting rid of the row operations...
Any suggestions are appreciated.
Thank you!
Julia Moeller
12-13-2007 01:54 PM - edited 12-13-2007 01:55 PM
Hello Julia!
Assuming you count the bits from the right starting with 1 then your examples don't look right to me. Can that be?
Here are my values:
45806 1011001011101110
7-9Bits 0000000111000000
AND 0000000011000000
>>7 0000000000000011 = 345703 1011001010000111
7-9Bits 0000000111000000
AND 0000000010000000
>>7 0000000000000010 = 245806 1011001011101110
13-14Bits 0011000000000000
AND 0011000000000000
>>13 0000000000000011 = 3
45703 1011001010000111
13-14Bits 0011000000000000
AND 0011000000000000
>>13 0000000000000011 = 3
And this is my solution to calculate what you want. In difference to the calculation above it first shift and than AND the values. n1 takes the index of the first bit, n2 the index of the second:
Option Explicit
Call GlobalDim( "n1" )
Call GlobalDim( "n2" )
Call GlobalDim( "n3" )
n1 = 7
n2 = 9
n3 = 2^(n2-n1+1)-1
Call ChnCalculate("Ch(""Results/Result1"" ) = Shr( Ch(""Group/Channel1"" ), n1-1) And n3")
Matthias
Matthias Alleweldt Project Engineer / Projektingenieur | Twigeater? |
12-14-2007 09:30 AM
Hi Julia,
DIAdem has a healthy array of bit manipulation functions that will do what you want-- look in the Help system under "Programming Reference>>Thematic Programming Reference>>Bit Functions". You can use the GetB() function to read out the value of a particular bit, or in your case if you want several bits worth I'd recommend using the AndB() function, where you need to provide the bitmask to AND with the binary data.
DIAdem stores all channel data values as DBLs, but these bit functions will operate on your channel data values as if they were stored as I32 values. DIAdem will also handle the conversion of your channel values to I32 and the result from the AndB() calculation back to DBLs. These bit manipulation functions are most often used in a FormulaCalc() expression. The one gotcha with the implicit I32 representation is that if your integer is greater than 2^31 you will have to jump through a few more hoops, because that would result in a negative I32 value.
Brad Turpin
DIAdem Product Support Engineer
National Instruments