November 28, 2020, 11:03:49 AM

Author Topic: [bmx] Bit Flags by rdodson41 [ 1+ years ago ]  (Read 549 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Bit Flags by rdodson41 [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Bit Flags
Author : rdodson41
Posted : 1+ years ago

Description : Bit flags are can be used to set parameters for a function. The value that is inputed as a parameter when calling the function is an integer, which is the sum of all the bits of parameters you want. For example:

In the B+ function create window, there are many different style flags you can have. You pick which styles you want, and add up there bit values. This is the integer that is used as the flag. The default is 15, which would mean the window has a title bar, is resizable, has a menu, and has a status bar.

This is useful for many things, so have fun with it!

Code :
Code: BlitzMax
  1. Repeat
  2.         Print
  3.         flag=Int(Input$("Flag:>"))
  4.         bit=Int(Input$("Bit:>"))
  5.         Select bitflag(flag,bit)
  6.                 Case 0
  7.                         Print "Bit "+bit+" has not been found in flag "+flag+"."
  8.                 Case 1
  9.                         Print "Bit "+bit+" has been found in flag "+flag+"."
  10.         End Select
  11. Forever
  15. Function bitflag(flag,bit)
  16.         p=0
  17.         Repeat
  18.                 If 2^p>11 Then Exit
  19.                 p:+1
  20.         Forever
  21.         p:-1   
  22.         For i=p To 0 Step -1
  23.                 If flag-(2^i)>=0 Then
  24.                         flag:-(2^i)
  25.                         If 2^i=bit Then Return 1
  26.                 End If
  27.         Next
  28.         Return 0
  29. End Function

Comments :

Koriolis(Posted 1+ years ago)

 There is far more simple:
Code: [Select]
Function bitflag(flag,bit)
Return (flag Shr bit) And 1
End Function
Note: in this version 'bit' starts at 0, not 1 (if you want it 1 base just do "Return (flag Shr (bit-1)) And 1")

rdodson41(Posted 1+ years ago)

 Oh, huh... and i spent and hour trying to figure this one out...

rdodson41(Posted 1+ years ago)

 It would be this:
Code: [Select]
Function BitFlag(flag,bit)
Return (flag & bit)=bit
End Function
Unfortunatly 'And' is only a logical opperater, not a bitwise opperator. So you have to use the & symbol.

Techlord(Posted 1+ years ago)

 A working Example.
Code: [Select]
;flags (multiple of 2)



;evaluate; use '/' and If's only.
If fx/fullbright And 1 Print("fullbright") ;do stuff
If fx/vertexcolors And 1  Print("vertexcolors")
If fx/flatshaded And 1 Print("flatshaded")
If fx/disablefog And 1 Print("disablefog")
If fx/disablebackfaceculling And 1 Print("disablebackfaceculling")
If fx/forcealphablending And 1 Print("forcealphablending")


SimplePortal 2.3.6 © 2008-2014, SimplePortal