Guest Wuchi Posted October 25, 2005 Share Posted October 25, 2005 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 Link to comment Share on other sites More sharing options...
LabJack Support Posted October 25, 2005 Share Posted October 25, 2005 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. Link to comment Share on other sites More sharing options...
AzeoTech Posted October 25, 2005 Share Posted October 25, 2005 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. Link to comment Share on other sites More sharing options...
wuchi Posted October 26, 2005 Share Posted October 26, 2005 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 Link to comment Share on other sites More sharing options...
AzeoTech Posted October 26, 2005 Share Posted October 26, 2005 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 Link to comment Share on other sites More sharing options...
wuchi Posted October 27, 2005 Share Posted October 27, 2005 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 Link to comment Share on other sites More sharing options...
AzeoTech Posted March 3, 2006 Share Posted March 3, 2006 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.