PDA

View Full Version : inverting all bits in a byte



queenidog
- 20th May 2018, 00:07
How do I invert all the bits in a byte?

Eg %011010110 would become %100101001

I assumed, then read it was by using the tilde (~) function but I'm having trouble using it in code.

eg if I had byteA=%011010110

and I did this: byteA=~byteA, shouldn't ~byteA display %100101001?

Strangely I can display the value of byteA in the display (LabX1) but ~byteA comes up empty.

richard
- 20th May 2018, 00:45
how many bits in a byte ? not 9



Eg %011010110 would become %100101001



Strangely I can display the value of byteA in the display (LabX1) but ~byteA comes up empty.

what display? , how displayed ?
if its a common garden variety lcd , chrs in this range 127 < chr >160 are not in the built in font
so using var A = $66
A will print on lcd
~A ($99) will not print on lcd

mpgmike
- 20th May 2018, 14:21
There is a simple ASM command, COMF. This is what it would look like:

@ BANKSEL _byteA
@ COMF _byteA, F

The first line moves the BSR (Bank Select Register) to the BANK containing your variable. The variable is preceded by an underscore for ASM; _byteA.

The second line executes the command, Compliment F (F is your variable). The appending ", F" puts the result back in your variable, byteA.

Hope this helps

Dick Ivers
- 20th May 2018, 22:44
Use the ^ operator for Bitwise Exclusive OR

ByteA = ByteA ^ %11111111 Inverts the bits of ByteA

queenidog
- 23rd May 2018, 23:20
thanks Dick

queenidog
- 23rd May 2018, 23:21
I have the new "enhanced" byte, with 9 bits.

Art
- 26th May 2018, 08:50
Using comf would be about twice as fast there since you don’t have to do anything at all with the literal 0xFF.

sayzer
- 30th May 2018, 06:45
Why not just use grandpa's way:

byte = 255- byte

Inverts the bits.