DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert decimal number to 16 bit binary

Hello,
 
What is the best way to convert a decimal number to a 16 bit binary representation, extract bits 7-9 from the 16 bit binary representation, and then convert the three continuous extracted bits into a decimal number?
 
For example:
45806 is represented as 1011001011101110. The bits 7-9 (counting from the right) are 011 which equals 3 in decimal format. So I want the function to receive 45806 and range 7-9 then return the value 3.

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

0 Kudos
Message 1 of 3
(17,336 Views)

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 = 3

45703      1011001010000111
7-9Bits    0000000111000000
AND        0000000010000000
>>7        0000000000000010 = 2

45806      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



Message Edited by Twigeater on 12-13-2007 08:55 PM
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 2 of 3
(17,332 Views)

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

0 Kudos
Message 3 of 3
(17,317 Views)