How to use bitwise 'AND'

manie

Senior Member
Picaxe = 28x1 OR 40x1.
I need to set three 'alarms' either On or Off. I thought of using one byte for this called 'Albits' (=b16). I want to use the three lower bits example: if alarms 3 and 1 are ON and 2=Off then the byte would look like this - %00000101, correct ?

If an alarm situation is recognised for alarm-2, and I want to set its bit to on (set to %00000111) but also retain the other two already on, how do I use the '&' command ? Same applies if the alarm-2 situation is cleared and must be reset (set to %00000101) but alarms 3 and 1 are still active.

I've looked in manual 2 (Basic Commands) but can't quite see how to do this. How do I apply the mask to retain the current bits as they are but change the one bit, other than pumping in numbers such as 'albits = 5' (%00000101) ?

Help me please.
Manie
 

hippy

Ex-Staff (retired)
To set a bit in a byte you would use OR ...

b16 = %00000101
b16 = b16 OR %00000010

To clear a bit, you would AND with the inverse of the bit

b16 = %00000111
b16 = b16 AND %11111101

An alternative is to allocate b0 as the set of bits and just use 'bit1=0' or 'bit1=1' as appropriate.

You can do similar with any byte register which contains bits, b0 and b1 on all PICAXE's, also b2 and b3 on the X1 and X2. Use symbol to name the bits appropriately ...

Symbol alarms = b1
Symbol alarm1 = bit8
Symbol alarm2 = bit9
Symbol alarm3 = bit10

alarm1 = 1
alarm2 = 0
alarm3 = 1
 
Last edited:

manie

Senior Member
Thank the Lord for friends like you Hippy ! Its so simple if you know isn't it ? Thanks, now I can code again....
Edit:
In the same vane, I'm using Hserin (success there...), which of the "other" system Flags is available to me for use ? I MIGHT use Interrupt later on but not now.
Thanks
 
Last edited:
Top