Sign in to follow this  
Guest Wuchi

Selecting a single bit in a string

Recommended Posts

Guest Wuchi

Hi again :(

I managed somehow to transform an integer to binary now the problem is that i have select for an example single bit for example if i have 10101 i want to select the third bit that is 1 and get new variable that says to me that the answer is 1 or 0. The thing is that now i have string is there a way to transform it to binar (0b...)

Best regards,

Wuchi

Share this post


Link to post
Share on other sites

Why don't you test the original integer rather than the string, then you can just use the same TestBit() function to determine whether a specified bit is set or clear.

Share this post


Link to post
Share on other sites

As LabJack Support mentioned, the easiest way would be to use original integer with the TestBit() function rather than the string.

However, if you prefer to use the string, there is a way to do it with the Evaluate() and Mid() functions. For example: if your string is Private.strBinary and you want the third bit do something like this.

Var.thirdbit = Evaluate(Mid(Private.strBinary,2,1))

The Mid() function returns a subset from the center of the string. From the above code it will look skip the first two characters (2) and then return a single (1) character. The Evaluate function converts the character from a string to a value that you can use in mathematical functions. You may or may not need the Evaluate function.

Share this post


Link to post
Share on other sites

Hi!

Me again :(

It is a great idea but i must write a function that stores every bit to a field array. That would be a simple solution but how can it be done on each % with 2 i have to write the result to var.test[0] for example the first one...

Wuchi

Share this post


Link to post
Share on other sites

A simple sequence such as the following will store the bits to an array in the variable Var.Binary.

Var.Decimal = 255
Private.j = 0
for (Private.i = 7, Private.i >= 0, Private.i--)
   Var.Binary[Private.j] = TestBit(Var.Decimal,Private.i)
   Private.j++
endfor

Share this post


Link to post
Share on other sites

Hi!

I have found an even better solution here it is:

I get an integer of for an example 404 if i want to look for the third bit i just do the

simple operation:

because you know how it goes: 404 % 2 = 0

202 % 2 = 0

101 % 2 = 1

The simpliest solution then is for the third bit: (404/4)%2

You get the 4 when you look the first line and the second line - you shift and then just % 2 for the third line. I works great i have tested it.

Thx for the answer it can get really helpful in the future.

Wuchi

Share this post


Link to post
Share on other sites

Yes, that will work, though it is a bit round-about and doesn't have much flexibility. There are many others. Really, though, the TestBit() function is the best and fastest way to determine the state of any bit in an integer if for no other reason then all that code is in C++ rather than in DAQFactory script. C++ code is much faster than DAQFactory script, so any time there is a DAQFactory function to do something you should use that rather than multiple DAQFactory operations. Another example of this is using array math. For example, if you wanted to find the maximum value of a channel you could do:

private.max = -999
for (private.x = 0, private.x < NumRows(MyChan), private.x++)
   if (MyChan[Private.x] > private.max)
      private.max = MyChan[Private.x]
   endif
endfor

but that would be really, really, slow compared to this:

private.max = Max(MyChan)

Here's another example. Lets say you want to see if array1 and array2 contain the same values. You could create a loop like the max one I showed above, or you could do:

private.match = Min(array1 == array2)

This is a little more advanced, but is a good example of where a little creativity can yield a dramatic speed benefit over loops. In this case, the part "array1 == array2" will generate a third array containing 0's and 1's. A 0 whenever the matching element of array1 does not equal array2, and 1 when it does. If all the elements of array1 are identical to array2, then array1 == array2 will give an array of all 1's. If some don't match, there will be 1's and 0's, and if none match, all 0's. So, we then take the Min of this array. If there are any 0's, meaning at least one of the elements don't match, the Min is 0. But if all the elements match, the array only contains 1's so the Min is 1.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this