# Toggle MB vector

## Recommended Posts

• MVP 2023

Can anyone think of a clever way to toggle every bit in a vector of 25 MBs in a single ladder rung and a single PLC scan?

##### Share on other sites

• MVP 2023

Num to BIT  i use for  init  some bit vector.

Fast way - Vector copy from XB to MB.  I use power on value for XB and copy this value to MB with one vector copy command.

##### Share on other sites

• MVP 2023

No, I need to TOGGLE each bit in the vector. Some of the bits are on and some are off. I need to flip each bit. It doesn't have to be immediate, just in a single scan. I can brute force it by copying each bit to another vector with an inverted contact and coil (x25), requiring 4 rungs, perhaps fewer, but I was just looking for a clever way to do it without all that typing.

Or maybe I'm misunderstanding what you're suggesting.

I'm thinking I can convert the vector to an integer, multiply by -1, then subtract 1, then convert back to a bit vector. I'll see if that works.

##### Share on other sites

• MVP 2023
22 minutes ago, Flex727 said:

I'm thinking I can convert the vector to an integer, multiply by -1, then subtract 1, then convert back to a bit vector. I'll see if that works.

This works perfectly. I think I learned this in middle school math and totally forgot.

• 1
##### Share on other sites

• MVP 2023

Maybe I didn't understand the task at first and thought that you need to quickly turn the bits off or on. I liked your idea with the math.

However, there was an idea in my head that the function could be done differently.

The result is in this piece of code

I will also add a test project for those who want to try to do this action in another way.

##### Share on other sites

• MVP 2023

I like your method, kratmel! Here's what I ended up doing:

The original bit was an "enabled" bit that I needed to use to hide an HMI element, so it needed to be flipped. Normally that's not a problem, but when you have 25 of them it's a pain. I have begged Unitronics for a checkbox on the HMI element hide setting to allow either on or off to hide the element, but I'm sure we'll never see it.

##### Share on other sites

• MVP 2023

I haven't tested this, but couldn't you use Toggle Bit in Vector for this?  No other mucking around needed.

cheers, Aus

##### Share on other sites

• MVP 2023
46 minutes ago, Ausman said:

I haven't tested this, but couldn't you use Toggle Bit in Vector for this?

I wanted to toggle ALL the bits. Toggle Bit in Vector only does one.

##### Share on other sites

• MVP 2023

Ahh yes, sorry.  I've used it within Vectors in the past and mis-remembered  that it allowed a range to be done.

I'm putting a trademark on mis-remembered.  But I'll allow usage here on the forum.  We can make it the person over 40s version of "mybad", which unfortunately covers a host of frustrating actions that have little responsibility attached.  🙂

cheers, Aus

## Create an account

Register a new account

×