How to invert just a bit - complement of a bit

# Thread: How to invert just a bit - complement of a bit – 339 days old

1. 0

## 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.

2. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

3. 0

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

btemp = %00000011
btemp.1 = ~btemp.0
this equals 1 after invert

4. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

5. 0

## 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```

6. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

7. 0

## 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.

8. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

9. 0

## 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.

10. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

11. 0

## 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

12. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues