LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to change a specific bit to 0 or 1?

Hi Natasha,

 


@Natasha1994 wrote:

Will it also work in a reverse order ? To change 0 to 1 ? 


Sure - as soon as you use the correct mask and the correct boolean operation (as mentioned above)…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 21 of 30
(726 Views)

Bitwise and shift operators - perform boolean (AND, NOT, OR, XOR) and shift operations on individual...

C# reference, but the logic is the same

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 22 of 30
(718 Views)

Here's what I might do:

 

altenbach_0-1692287467705.png

 

0 Kudos
Message 23 of 30
(707 Views)

Here's a more general version where you can freely select all the bits you like to toggle:

 

altenbach_0-1692289568959.png

 

 

0 Kudos
Message 24 of 30
(692 Views)

Because I am TERRIBLE at bitwise operations, I usually translate the numbers into Booleans so I can manipulate them "directly".  It is more important to me that the results are accurate than me trying to be efficient.  I know most people prefer to manipulate the numbers directly using bitwise operations.  I just have a mental block about them.

 

And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.

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 25 of 30
(686 Views)

@billko wrote:

And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.


Disagree! I've seen plenty of cases where boolean operation were incorrect (especially here in the forum, not with my own code!, of course!), OTOH, bitwise operations on unsigned integers are identical to boolean operations. If they are incorrect, the underlying boolean operation was already incorrect. 😄

 

Substituting boolean operations for bitwise, gives you a dramatic penalty (8x more memory per bit!), and adding the conversion overhead adds to the expense.

0 Kudos
Message 26 of 30
(681 Views)

@altenbach wrote:

@billko wrote:

And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.


Disagree! I've seen plenty of cases where boolean operation were incorrect (especially here in the forum, not with my own code!, of course!), OTOH, bitwise operations on unsigned integers are identical to boolean operations. If they are incorrect, the underlying boolean operation was already incorrect. 😄

 

Substituting boolean operations for bitwise, gives you a dramatic penalty (8x more memory per bit!), and adding the conversion overhead adds to the expense.


I think of it as calculating which switch to throw vs just looking at the switches and throwing the right one.  And unless I am going to do this 1000x in a FOR loop, I don't really care if it takes a split second longer or multiplies my memory usage by 8x per bit.

 

To me, it's much easier to visualize which bit to flip than to calculate it.  And I've seen bad mask calculations a lot more than bit miswirings.

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 27 of 30
(663 Views)

@billko wrote:

Because I am TERRIBLE at bitwise operations, I usually translate the numbers into Booleans so I can manipulate them "directly".  It is more important to me that the results are accurate than me trying to be efficient.  I know most people prefer to manipulate the numbers directly using bitwise operations.  I just have a mental block about them.

 

And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.


I do that in a develop/test stage many times, or just use a binary display of a number.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 28 of 30
(636 Views)

@Yamaeda wrote:

@billko wrote:

Because I am TERRIBLE at bitwise operations, I usually translate the numbers into Booleans so I can manipulate them "directly".  It is more important to me that the results are accurate than me trying to be efficient.  I know most people prefer to manipulate the numbers directly using bitwise operations.  I just have a mental block about them.

 

And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.


I do that in a develop/test stage many times, or just use a binary display of a number.


I never thought of that!!!  I could do an explicit Boolean conversion to check my blind spot bitwise operation for validity!  Then I could have the best of both worlds.

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 29 of 30
(633 Views)

@billko wrote:

@Yamaeda wrote:

I do that in a develop/test stage many times, or just use a binary display of a number.


I never thought of that!!!  I could do an explicit Boolean conversion to check my blind spot bitwise operation for validity!  Then I could have the best of both worlds.


I also like the binary display when dealing with bit-wise logic.  It works well for 8 or 16-bit integers.  Once going for a 32-bit or 64-bit, it becomes unwieldy and I switch to hex display.


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 30 of 30
(624 Views)