LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to change bit in U32 depending on state

Hello,

how can this be done:

 

For example:

U32_In is: 00000000000000000000000000000000 (32x 0)

I32_Number: 4

Bool_State. True

 

Then U32_out should be 00000000000000000000000000010000

 

Thanks for help

0 Kudos
Message 1 of 13
(3,763 Views)

Just use the boolean OR function to bring in that type of logic.


Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 2 of 13
(3,759 Views)

Simple way: Number to boolean array, replace array subset, boolean array to number.

 

More efficient way: Bit shifting (shift left, put in your 'True' and then shift right)

 

Boolean Logic way: Use your new number to create a bit mask and logic AND with the U32


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 3 of 13
(3,757 Views)

Sorry for nitpicking, but


@Sam_Sharp wrote:

Simple way: Number to boolean array, replace array subset, boolean array to number.

 [...]


Waste of performance

 


@Sam_Sharp wrote:
[...]

More efficient way: Bit shifting (shift left, put in your 'True' and then shift right)

 [...]


Helps only for numbers which already have the correct pattern but the pattern is in the wrong location. Btw: Bitshifting is a multiplication or devision (depending on the direction of the shift) by two. So if the result should NOT be a value which is a multiplication/devision by 2^n, this won't work!

 


@Sam_Sharp wrote:
[...]

Boolean Logic way: Use your new number to create a bit mask and logic AND with the U32


AND is the wrong combination. The correct one, as i understand the original question, is OR.

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 4 of 13
(3,752 Views)

Ok, the AND makes a 1 at the position.

But how to make a 0 at the specified bit-position?

0 Kudos
Message 5 of 13
(3,749 Views)

Re-reading the original question, i have a hard time to understand the desired pattern.

4 in binary would be 100, not 10000, so it seems to require a boolean OR then shifted TWO BITS to the left.

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 6 of 13
(3,745 Views)

>Re-reading the original question, i have a hard time to understand the desired pattern.

 

The 4 stands for bitnumber four. Starting with index 0 it is at position 4. Thats what the 10000 stands for.

The boolean whould then switch this bitnumber 4 to 1 (true) or 0 (false).

 

0 Kudos
Message 7 of 13
(3,736 Views)

Yes, the number/boolean array is inefficient, but it's also the easiest to understand exactly what's happening to a novice programmer.

 

You were right about needing to OR though - I haven't had enough coffee yet this morning!

 

As for the bit-shifting...sorry, I meant rotate with carry...you can do that as long as you hold the bits you pull out and then put them back again. You rotate right (storing the bits), and then rotate left inserting your new bit and then the remaining bits that you pulled out earlier.

 

I was merely proposing that there are a number of different methods for achieving the same thing.

 

Also, what he wants is to replace the 4th bit from the LSB, not replace it with a binary 4. Needs to use a mask of 2^4, not 4.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 8 of 13
(3,734 Views)

That kind of operation is best done with using the Logical Shift. Connect a constant 1 to the x input, the I32 numer of bits to shift to y and you get your bit pattern output. Then if you want to change the bit in an incoming integer you have to use different boolean logic depending if you want to set or clear that bit.

 

To set it you simply OR your input number with the bit pattern, to clear it you AND it with the inverse (Invert node on the Boolean Palette) of the bit pattern.

 

If the shift (your number 4) is constant and does not need to be parametrized you can also replace the Logical Shift through a fixed numeric constant of in this case hexadecimal 0x10 or decimal 16.

Rolf Kalbermatter
My Blog
Message 9 of 13
(3,728 Views)

@Sam: Yes, i had a misunderstanding on that point. I thought the OP wanted to combine the numbers, not specifying a bit to switch to the boolean value.

 

 

Attached a solution i think does what the OP wants (despite i very seldom post "working" solutions!).

But i would expect altenbach to come up with a far better solution 🙂

 

Norbert

 

 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 10 of 13
(3,719 Views)