LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Boolean logic : Enabling & Disabling Bit

Solved!
Go to solution

sticyfinger wrote: 

@ Nathand

I did that in the past but I totally forgot that a negated number is negative. Plus I forgot that the number I was looking at was 32bits. L long week.


Not sure what you mean by this. If you negate an unsigned value, the result is an unsigned value with all the bits inverted. There's no need for all the To I32 conversions in your code. If you're only doing logical operations, and not mathematical ones, then it doesn't matter whether you use signed or unsigned values, either way you get the same series of bits. (Actually, due to the way two's complement works, even if you're doing math you'll often still get the same series of bits regardless of whether the values are signed - they'll just display differently on screen.)

0 Kudos
Message 11 of 22
(1,700 Views)

I'm not sure whos is more efficient but here is another way to do the same thing with either the boolean array, or a number.

0 Kudos
Message 12 of 22
(1,694 Views)

For completeness in documenting bitwise boolean operations.

 

If you want to toggle a bit (i.e. turn on if off, or turn off if on, not knowing what the original state is or what it will be come, just that you want to change it), than you XOR it with a number that has that bit set to 1.  XORing a bit with 0 is "leave that bit alone".  XORing a bit with 1 is "flip that bit".

 

Note:  I edited my description to be a bit more clear.

Message 13 of 22
(1,684 Views)

 

******************************************

Actually since you are starting with a U8 for the "command", I would continue using that and use a U8 constant and final indicator.  It's not logical for me to be thinking about the extra 24 bits in a U32 or I32 when I'm only dealing with an 8-bit command value.  I'd be worried that mixing and matching other integer datatypes through carelessness would give me results I wasn't expecting.  Try to be consistent in representation, and when by accident you miss changing the type of an integer, you'd see where your problems might be by way of the coercion dot.

********************************************

 

 

@ Raven, 

 

 

yeah that make perfect sense. Thanks. 

 

0 Kudos
Message 14 of 22
(1,681 Views)

@Hooovahh wrote:

I'm not sure whos is more efficient but here is another way to do the same thing with either the boolean array, or a number.



You can safely assume that boolean arrays NEVER can be faster than a single number. A boolean array is an array of U8 with one U8 per bit. A number is simply a register. Go figure how much CPU operations each of them requires to perform a boolean operation on the whole thing! Smiley Very Happy

Rolf Kalbermatter
My Blog
Message 15 of 22
(1,668 Views)

@billko wrote:

I'm much better at visualizing flipping bits, so I convert to an array of booleans, flip the appropriate bit and convert it back to a number.  I know it's not efficient, but for me I'm a lot less error-prone if I do it that way.  I must have some kind of mental block.


Binary radix helps in that situation.


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 16 of 22
(1,655 Views)

@crossrulz wrote:

@billko wrote:

I'm much better at visualizing flipping bits, so I convert to an array of booleans, flip the appropriate bit and convert it back to a number.  I know it's not efficient, but for me I'm a lot less error-prone if I do it that way.  I must have some kind of mental block.


Binary radix helps in that situation.


I've actually done that where I could, but sometimes the numbers are outputs and not contants.  I envy people who can do this stuff inside their heads.  Maybe it's like learning multiplication tables.  You just have to do it often enough where it just becomes automatic.

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 17 of 22
(1,648 Views)

@billko wrote:

I've actually done that where I could, but sometimes the numbers are outputs and not contants.  I envy people who can do this stuff inside their heads.  Maybe it's like learning multiplication tables.  You just have to do it often enough where it just becomes automatic.


Well off-topic, but have you taught yourself to count in binary on your fingers? I've found it helps: for example you can read a number on screen, do it on your fingers, and see quickly which bits are on and which are off. Of course you're limited to 10 bits, and using more than 5 is inconvenient, but it is nice to be able to count to 31 on one hand. You can of course do this in either direction, but I start with my thumb as 1, forefinger as 2, etc up to the pinkie as 16.

Message 18 of 22
(1,643 Views)

@billko wrote:

@crossrulz wrote:

Binary radix helps in that situation.


I've actually done that where I could, but sometimes the numbers are outputs and not contants.


It works for controls and indicators as well.

 

Personally, I use the Hex radix a lot.  It's just a little easier to read than a big string of 0s and 1s.


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 19 of 22
(1,640 Views)

@crossrulz wrote:

@billko wrote:

@crossrulz wrote:

Binary radix helps in that situation.


I've actually done that where I could, but sometimes the numbers are outputs and not contants.


It works for controls and indicators as well.

 

Personally, I use the Hex radix a lot.  It's just a little easier to read than a big string of 0s and 1s.


Sorry I've forced you to wander off-topic about this.  I understand about controls and indicators, I meant outputs from calculations.  I appreciate everyone's help and I'm sorry - I didn't mean to hijack the thread.  😞

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 20 of 22
(1,638 Views)