From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to choose bits from a hexadecimal number?

Solved!
Go to solution

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).

0 Kudos
Message 1 of 10
(5,611 Views)
Solution
Accepted by topic author aneps

A little bit manipulation will get you there.  The Logical Shift and AND functions are key when dealing with this kind of problem.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 10
(5,594 Views)

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:

 

  1. Convert your text number to a single integer (32bit or 64bit, depending upon how big the actual number is)
  2. Create a 9-bit bit mask.  This is a number with 9 bits set to 1 and all the rest set to 0.  Use the same type (U64 or U32) as you did in the previous step.  Note that this is very easy to do if you set the display format to binary and use a constant.
  3. For the right-most 9 bits, do a logical AND of the 9-bit mast and your number.  This will set bits 10-x to 0, leaving you with a number representing the low 9 bits.  You can save it as a U16, if you want, to save space.
  4. For the next 9 bits, left-shift your mask 9 bits, then do a logical AND with the original number.  Now right shift the result 9 bits.  You now have a number representing the second set of nine bits.
  5. If you have a three or more 9-bit sets you need to look at, simply left-shift your mask the appropriate number of bits, do a logical AND, then right-shift the result.

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!

0 Kudos
Message 3 of 10
(5,587 Views)

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/

0 Kudos
Message 4 of 10
(4,588 Views)

@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. 🙂

Message 5 of 10
(4,584 Views)

@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.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 6 of 10
(4,583 Views)

@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


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 7 of 10
(4,573 Views)

@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!

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 8 of 10
(4,564 Views)

@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. 😄

0 Kudos
Message 9 of 10
(4,561 Views)

@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.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 10 of 10
(4,537 Views)