bptr for bits?

Hemi345

Senior Member
I'm storing some statuses as bits in byte b3. Quite a few of these are configured the same way, so if I determine which menu option I'm currently on, I can toggle on/off the corresponding bit. Is there a way to dynamically reference a bit? I think there's gotta be a way to make the following IF statement more efficient.

Code:
symbol cREa                = 32 'anti-clockwise rotation of encoder
symbol cREc                = 64 'clockwise rotation
symbol cREi                = 96 'idle, no rotation

symbol vMenuOption = b0
symbol bMenuChoose    = bit24            'in b3
symbol bNeedTune        = bit25            'in b3
symbol bAlarm1stat    = bit26            'in b3
symbol bAlarm2stat    = bit27            'in b3

    if vMenuOption = 1 AND vStatus = cREa then 'AW
        bAlarm1stat = cOff
    else if vMenuOption = 1 AND vStatus = cREc then 'CW
        bAlarm1stat = cOn
    else if vMenuOption = 2 AND vStatus = cREa then 'AW
        bAlarm2stat = cOff
    else if vMenuOption = 2 AND vStatus = cREc then 'CW
        bAlarm2stat = cOn
    end if
Is there a clever way to toggle the bits using something like the following example to make it more concise and dynamic?

Code:
bptr = vMenuOption + 60
if vStatus = cREa then
    @bptr = cOff
else if vStatus = cREc then
    @bptr = cOn
end if
 

Aries

New Member
You could try something like this ...
Code:
    lookup vMenuOption,(0,%00000100,%00001000),b2
    if vStatus = cREa then 'AW
        b3 = NOT b2 & b3
    else if vStatus = cREc then 'CW
        b3 = b2 | b3
    end if
the lookup has the relevant bit corresponding to the menu option (0 is a dummy if you don't have menu 0)
then you either set (with OR) or clear (with AND on the NOTted version) .
 

hippy

Technical Support
Staff member
I would probably go with ...
Code:
If vStatus <> cREi Then
  Select Case vMenuOption
    Case 1 : bAlarm1stat = vStatus / cREc
    Case 2 : bAlarm2stat = vStatus / cREc
  End Select
End If
Pre-calculating "vStatus / cREc" if there were a lot of those ...
Code:
If vStatus <> cREi Then
  bSetOrClr = vStatus / cREc
  Select Case vMenuOption
    Case 1 : bAlarm1stat = bSetOrClr
    Case 2 : bAlarm2stat = bSetOrClr
  End Select
End If
 

Hemi345

Senior Member
Thank you both for the suggestions. I have 6 options I need to toggle, so I'll try both out and see which one saves the most program space (y)

Have a happy new year! 🍾
 
Top