09-11-2013 07:21 AM - edited 09-11-2013 07:30 AM
I have a list of hexadecimal number (one column with 'n' rows). Let the list be named 'A'. Each hexadecimal number in A, after conversion to Binary, I need to choose 9 bits from the Right end (d0, d1, d2 ..d8) and label as X. Then the next consecutive 9 bits (d9,d10,...d17) and label as Y. I want to do this for all the 'n' elements in A and put it in separate X and Y columns. Can anyone help me to do this? Thanks a lot.
Example:
Suppose a=11110000111100000111110000 is binary form of the first element in A. Then, I want to choose bits so that, X=111110000 (bits from the right end of a) and Y=111100000 (the successive 9 bits). I don't care the rest of the bits towards left. In A, I have 'n' number of elements (a1, a2, a3,...an). I want to do this for all and make a list of X and Y (two separate columns).
Solved! Go to Solution.
09-11-2013 07:37 AM - edited 09-11-2013 07:37 AM
A little bit manipulation will get you there. The Logical Shift and AND functions are key when dealing with this kind of problem.
09-11-2013 07:39 AM
18 bits does not add up when using hexadecimal, since each digit is 4 bits. Regardless, the algorithm is the same. For each row, do the following:
The Logical Shift primitive, which does right and left shifts, can be found under the Mathematics>>Numeric>>Data Manipulation palette. The logical AND is found in the Boolean palette, and is more commonly used for Boolean data types, but works quite well for bitwise operations on integers.
There are a plethora of ways this could be done, but this is probably the most efficient. For more efficiency, generate you bit-shifted masks in advance and don't keep regenerating them.
Good luck!
09-17-2018 12:31 PM
Please compare the efficiency of the shifting method to using the number to binary array function. https://zone.ni.com/reference/en-XX/help/371361H-01/glang/number_to_boolean_array/
09-17-2018 12:42 PM
@Jandy wrote:
Please compare the efficiency of the shifting method to using the number to binary array function.
You posted to a thread that has not seen activity in five years with a demand that you can easily test yourself. Did you try?
Hint: Doing bitwise operations on an integer will be about an order of magnitude faster than converting to a different data structure (boolean array) that takes up 8x more memory. Seems obvious. This is one of the situations where "going green" is not advisable. 🙂
09-17-2018 12:46 PM
@Jandy wrote:
Please compare the efficiency of the shifting method to using the number to binary array function. https://zone.ni.com/reference/en-XX/help/371361H-01/glang/number_to_boolean_array/
I prefer this method because I can't picture bitwise operations in my head. It's not second-nature to me. However, representing the number as an array of bits and then splitting the array up as needed is exactly analogous to the description of the process, itself, and makes it a lot easier for me to understand.
Note that I said, "Easier for me to understand." This is because I suspect that the vast majority of developers are more comfortable with bitwise operations than me.
09-17-2018 01:25 PM
@billko wrote:I prefer this method because I can't picture bitwise operations in my head.
Something that helps is setting the display format of your controls, indicators, and constants to be in hex or binary. I typically use "%02x" for bytes, but could just as easily use "%08b" for the display style. This way you can see all of the bits and get the performance boost of using bitwise arithmetic. Of course, make sure your radix is visible when you do this.
Here is a tool to help set the display style for all of your controls, indicators, and constants: Format Numeric QuickDrop
09-17-2018 03:08 PM
@crossrulz wrote:
@billko wrote:I prefer this method because I can't picture bitwise operations in my head.
Something that helps is setting the display format of your controls, indicators, and constants to be in hex or binary. I typically use "%02x" for bytes, but could just as easily use "%08b" for the display style. This way you can see all of the bits and get the performance boost of using bitwise arithmetic. Of course, make sure your radix is visible when you do this.
Here is a tool to help set the display style for all of your controls, indicators, and constants: Format Numeric QuickDrop
Why didn't I think of that? Thanks!
09-17-2018 03:33 PM
@billko wrote:
However, representing the number as an array of bits and then splitting the array up as needed is exactly analogous to the description of the process, itself, and makes it a lot easier for me to understand..
Boolean arrays can be confusing too, especially since if you display it in a horizontal LED array indicator, the LSB is on the left, while in a binary formatted integer, the LSB is on the right. 😄
09-18-2018 06:50 AM
@altenbach wrote:
@billko wrote:
However, representing the number as an array of bits and then splitting the array up as needed is exactly analogous to the description of the process, itself, and makes it a lot easier for me to understand..
Boolean arrays can be confusing too, especially since if you display it in a horizontal LED array indicator, the LSB is on the left, while in a binary formatted integer, the LSB is on the right. 😄
LOL - yeah, I meant dealing with bitwise operations on the block diagram. The way I work around the display silliness is to display them as a cluster, instead. Usually I'm representing a number as an array of Booleans, so I know how many elements to expect. Since they usually represent flags, a cluster has the added benefit of unique labels for each bit.