Simple VAR question
Joms
Posts: 279
I am trying to invert a variable.· I am fairly sure it is pretty simple, but I just can't seem to figure it out.· I have a variable that I access at bit level and want to invert all of the bits....· Example...
%11111111··· I need this to equal·· ·%00000000
I have tried multiplying by -1 and also tried puting a minus sign in front of the number to make it a signed number, but it always seems to add a one when I make it a signed number.
I probably just don't understand what I am doing here, but if someone could chime in and give me a clue, that would be great...· Thanks in advance...
%11111111··· I need this to equal·· ·%00000000
I have tried multiplying by -1 and also tried puting a minus sign in front of the number to make it a signed number, but it always seems to add a one when I make it a signed number.
I probably just don't understand what I am doing here, but if someone could chime in and give me a clue, that would be great...· Thanks in advance...
Comments
You can also use exclusive or (^) to just invert some of the bits.
If X is a byte variable, you can write: X = !X
You can also write: X = X ^ %11111111
If you only want the least significant 7 bits inverted, you would write: X = X ^ %1111111
some_var = ~some_var
The is one PBASIC operator you can use to perform that task. Here is a description and example from the PBASIC Help file:
quote
~ Invert Bits
The Inverse operator (~) complements (inverts) the bits of a number. Each bit that contains a 1 is changed to 0 and each bit containing 0 is changed to 1. This process is also known as a "bitwise NOT" and one's compliment.
~0 = 1
~1 = 0
end quote
Variables operate the same way as the examples above.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When all else fails, try inserting a new battery.