From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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 can I set specific bits in a 16-bit integer?

Solved!
Go to solution

Hello everyone,

 

as the title says I need to modify or rather to set a specific bit in a string which then is sent to a motor. I need to be sure that my command is correct as I am experiencing troubles with that motor and need to identify if its source.

 

First of all my strings have to be in the Little Endian order. Then the structure of the string should be the following:

 

Change Velocity command ‘V’xxCR 056h + one unsigned short (16-bit) integer + 0Dh (Note: Uppercase ‘V’)

Note: The lower 15 bits (Bit 14 through 0) contain the velocity value. The high-order bit (Bit 15) is used to indicate the microstep-to-step resolution: 0 = 10, 1 = 50 uSteps/step.

 

Until now, I used Flatten To String to convert 32 bit integers into bytes of the correct order. I thought I could use the Join Numbers function, but that only works for at least 8 bit numbers and there is no "1 bit number". I searched for an option to build a a string and set the bits via a Boolean Cluster, but I did not really understand how to transfer this to my problem.

How can I build up the correct 16-bit integer (e.g. set the velocity to "10000" with a resolution of 50 µSteps/step)

 

I would like to add the "V" and the CR via Concatenate Strings to the 16-bit integer, but other possibilites are also welcome.

 

I have seens the examples for bit manipulation in C-code, but I wish to do this with LabView as I am not familiar with C,matlab and so on.

 

Thank you very much for your help!

0 Kudos
Message 1 of 6
(5,185 Views)
Solution
Accepted by topic author MrKSE

You really need to learn Boolean logic and how to shift bits around.

 

AND is really good for masking out bits (forcing them to 0) and OR is really good for adding bit values.  Then Logical Shift is used to get the bits in the right places before doing the AND and OR.

 

NOTE: Rate is an enum with 10 being a value of 0 and 50 being 1.


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 2 of 6
(5,162 Views)

Thank you very much for your quick answer.

 

Can I change the representation of velocity to "decimal", even if I use a hexadecimal constant for the AND operator?

 

Is there a tutorial for this kind of boolean logic? I looked up the functions but for me it only makes sense for single True/False objects. If I connect to U16 with or, will they be compared bitwise and if both bits are 0 the result is 0,otherwise 1, or what does it do?

 

 

However, I apologize for my lack of knowledge of Boolean logic and thank you again for your help!

0 Kudos
Message 3 of 6
(5,130 Views)

Hi KSE,

 

Can I change the representation of velocity to "decimal", even if I use a hexadecimal constant for the AND operator?

Yes. Representation in the display is purely cosmetics, the underlying value doesn't change…

 

Is there a tutorial for this kind of boolean logic?

Wikipedia?

 

If I connect to U16 with or, will they be compared bitwise and if both bits are 0 the result is 0,otherwise 1, or what does it do?

Read the help. It clearly says: This function performs bitwise operations on numeric inputs.

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 6
(5,122 Views)

MrKSE wrote:

Can I change the representation of velocity to "decimal", even if I use a hexadecimal constant for the AND operator?


When you do bit packing like this a lot, you want to see the hexidecimal representation.  It is much easier to verify the right bits were set/cleared that way.  As GerdW said, the hexidecimal vs decimal is just how the number is displayed; the actual data is the same.

 


MrKSE wrote:

Is there a tutorial for this kind of boolean logic? I looked up the functions but for me it only makes sense for single True/False objects. If I connect to U16 with or, will they be compared bitwise and if both bits are 0 the result is 0,otherwise 1, or what does it do?


Go to a digital electronics 101 course.  You will learn way more boolean logic than you ever wanted to see.  So I would recommend finding a good digital electronics textbook.


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 5 of 6
(5,115 Views)

Ah I see, it is emphasized as "Note"..Smiley Happy

 

I'd like to thank both of you for your fast help. Hopefully this string will solve my problems!

 

Kind regards,

KSE

 

 

0 Kudos
Message 6 of 6
(5,111 Views)