# How to invert just a bit - complement of a bit

• 26th June 2019, 15:48
kuhrig
How to invert just a bit - complement of a bit
I would like to get some help how to invert just a bit. I'm using the complement sign, but it does not work.

Example:

dim btemp as byte

btemp = 11

btemp.1 = ~btemp.0

Result:
btemp = 11

Does not work. Bit result of complement bit always 1.

Now I use:
If btemp.0 = 1 then
clear btemp.1
else
set btemp.1
endif

Thanks.
• 26th June 2019, 16:16
towlerg
Re: How to invert just a bit - complement of a bit
Quote:

btemp = %00000011
btemp.1 = ~btemp.0
this equals 1 after invert
• 26th June 2019, 21:47
RGV250
Re: How to invert just a bit - complement of a bit
Hi,
This works on the individual bits and you can simulate it in the free VSM
Code:

``` 'Simulates in PIC16_ALCD_VHB.DSN board     Include "Proton_4.Inc" ' ** Declare Variables **     Dim Testbits  As Byte     Dim Testbits1 As Byte     Testbits.0 = 1     Testbits.1 = 0     Testbits.3 = 1      'Used so preceding 0 shows     Testbits1.3 = 1    'Used so preceding 0 shows         Dim btemp As Byte             DelayMS 100     Cls MainLoop:     Print At 1,1,Bin Testbits    'Show initial state     Testbits1.0 = ~Testbits.0    'Invert bit 0     Testbits1.1 = ~Testbits.1    'Invert bit 1     Print At 2,1,Bin Testbits1    'Show inverted states         btemp = %00001011        Print At 1,6,Bin btemp    'Show inverted states        btemp.1 = ~btemp.0          Print At 2,6,Bin btemp    'Show inverted states     GoTo MainLoop```
• 27th June 2019, 20:26
top204
Re: How to invert just a bit - complement of a bit
I've just ran so me tests and teh inversion of bits using the complement operator is working OK.

On an 18F device it uses the Btg mnemonic, with 14-bit core devices it uses an Xor mnemonic if both bits are the same. Otherwise, it uses a set of mnemonics to test the condition of the source bit and set or clear the destination bit.
• 18th July 2019, 21:31
top204
Re: How to invert just a bit - complement of a bit
I'm ever so sorry. I forgot to look that this question was about the Proton24 compiler!

I'll do some tests and get back to you.
• 19th July 2019, 13:41
top204
Re: How to invert just a bit - complement of a bit
You are correct kuhrig, the complement of a bit that is different to the assignment bit is not working as it should. I have now corrected it and will produce an update to teh compilers.

It was caused by the Zero flag not being altered by some of the mnemonics used, even though the PIC24 datasheets state that it should be!

I've also added a few extras to the compiler updates. For example, A timeout label in serial commands can now also be Break or Continue, so when inside a loop, a timeout will either exit the loop, or skip what else is in the loop and continue it.

Dim Index as Byte = 0
Dim MyVar as Byte
Dim MyArray[20] as Byte

Clear MyArray

Do
HSerin 1000, Break, [MyVar] ' Exit the loop if timed out
If MyVar = 0 Then Break ' Exit the loop when a null received
MyArray[Index] = MyArray ' Place the byte received into the array
Loop