making 'power flags'

Blitz3D Forums/Blitz3D Beginners Area/making 'power flags'

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.


blank    = flag And 1
titlebar = flag And 2
minimize = flag And 4
 




That would only work if you wanted to set a single bit value(and clear out the rest) or to test if a bit has been set.

typicaly the way to use bit flags is to define some constants to test/set them with:

Const blank = 1    ;00000000000000000000000000000001
Const titlebar = 2 ;00000000000000000000000000000010
Const Minimize = 4 ;00000000000000000000000000000100


Then to set the bit value if it hasn't been set yet:

Function setbit%(flag%,bit%)
  
  If NOT flag AND bit then flag = flag OR bit
  
  Return flag
End Function


To specificly clear a bit:

Function clearbit%(flag%,bit%)
  
  If flag AND bit then flag = flag OR bit
  
  Return flag
End Function


To toggle a bit:

Function togglebit%(flag%,bit%)
  
  flag = flag OR bit
  
  Return flag
End Function


To test a bit:

Function testbit%(flag%,bit%)
  
  return flag AND bit

End Function


Then in your code you can just call on the functions:


Flag% = 0


;set the titlebar flag

flag = setbit(flag,titlebar)

;toggle the blank flag

flag = togglebit(flag,blank)

;test if minimize flag has been set

If testflag(flag,minimize) Then Print "Minimize flag has been set!"

;test if both titlebar and blank flags have been set

If testflag(flag,titlebar) And testflag(flag,blank) Then Print "titlebar And blank flags have been set!"

;toggle the blank flag (if off it turns it on, if on it turns it off)

flag = togglebit(flag,blank)

;test if the blank flag is off

If NOT testbit(flag,blank) Then Print "Blank flag is now off!"



Pretty simple...