News:

  • September 29, 2026, 07:02:01 PM

Login with username, password and session length

Author Topic: Inverting a bit  (Read 14562 times)

Andrew S

  • Hero Member
  • *****
  • Posts: 144
Inverting a bit
« on: August 16, 2019, 01:27:46 PM »
I'd love to see an instruction to invert a bit. I know we have power-flow invert (which I love and use daily) but since it only works for the first level I have to use more complex code when I have a condition that I need to satisfy before I do the invert.

I'm wondering if anyone has a clean solution for this: IF <some flag(s)> THEN <invert bit>

I can make it work but it's messier than I think it should be. My current solution is attached. Surely it can be done better?
Thanks, Andrew.
« Last Edit: August 16, 2019, 01:35:47 PM by Andrew S »

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3847
    • Host Engineering
Re: Inverting a bit
« Reply #1 on: August 16, 2019, 01:58:56 PM »
MATH C0 "!C0"

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3847
    • Host Engineering
Re: Inverting a bit
« Reply #2 on: August 16, 2019, 02:00:30 PM »
If you have a bank of bits you want to invert every bit that fits in a bit-cast (or is already numeric), use "~" which is  bit-size invert (! is logical NOT)
MATH C0:UB "~C0:UB"

Andrew S

  • Hero Member
  • *****
  • Posts: 144
Re: Inverting a bit
« Reply #3 on: August 16, 2019, 02:02:30 PM »
Fantastic. Just what I needed to know. I was sure it could be done better. Thank you.

Andrew.

Mike Nash

  • Hero Member
  • *****
  • Posts: 655
Re: Inverting a bit
« Reply #4 on: August 16, 2019, 02:27:06 PM »
If you actually want to use a "flag" bit, you can use something like MATH C0:UW X0:UW^C128:UW

C0 would contain X0, inverted if C128 is 1, not inverted if C128 is 0.

UW would give 16 contiguous bits. The casting caveat is the 16 bit boundaries for Word, etc.

You could also just use MATH C0 X0^C100 to do each individually.

Andrew S

  • Hero Member
  • *****
  • Posts: 144
Re: Inverting a bit
« Reply #5 on: August 16, 2019, 02:31:49 PM »
If you actually want to use a "flag" bit, you can use something like MATH C0:UW X0:UW^C128:UW

C0 would contain X0, inverted if C128 is 1, not inverted if C128 is 0.

UW would give 16 contiguous bits. The casting caveat is the 16 bit boundaries for Word, etc.

You could also just use MATH C0 X0^C100 to do each individually.

Very useful. Thanks Mike.

Mike Nash

  • Hero Member
  • *****
  • Posts: 655
Re: Inverting a bit
« Reply #6 on: August 16, 2019, 02:39:32 PM »
You're welcome.
If you are feeling particularly nasty, you can put all of the input inversion at the top of the program, before anything else uses it and do:


Andrew S

  • Hero Member
  • *****
  • Posts: 144
Re: Inverting a bit
« Reply #7 on: August 16, 2019, 02:46:02 PM »
You're welcome.
If you are feeling particularly nasty, you can put all of the input inversion at the top of the program, before anything else uses it and do:

That's where I came in, but I believe it won't invert X254 when it's off, only when it's on.

Mike Nash

  • Hero Member
  • *****
  • Posts: 655
Re: Inverting a bit
« Reply #8 on: August 16, 2019, 06:42:16 PM »
Amazing, I ran that on a real BRX and it looked to be working, but obviously not.

This does (I am almost certain!) The side benefit is that the coil can be another element entirely, so it doesn't have to make troubleshooting as difficult.

Andrew S

  • Hero Member
  • *****
  • Posts: 144
Re: Inverting a bit
« Reply #9 on: August 17, 2019, 02:50:48 AM »
Amazing, I ran that on a real BRX and it looked to be working, but obviously not.

This does (I am almost certain!) The side benefit is that the coil can be another element entirely, so it doesn't have to make troubleshooting as difficult.

I think you're right - that should work. I'd be leery of using an output coil in stages though since it gets turned off when I JMP out of (or otherwise leave) the stage.

This is an interesting exercise I think. Appreciate the solution Mike.

Andrew.