How to create flags?
BlitzMax Forums/BlitzMax Programming/How to create flags?
| ||
I'm working on a GUI for my game and I need to implement some flags. The problem is that I don't know how to check if the falg has 2 or 4 or 16 in it. Could someone explain a bit? |
| ||
You use the ampersand (&) to "and" a flag with your value. Example: SuperStrict Const flag_1:Int = $01 Const flag_2:Int = $02 Const flag_4:Int = $04 Const flag_8:Int = $08 Const flag_16:Int = $10 Const flag_32:Int = $20 Const flag_64:Int = $40 Const flag_128:Int = $80 Function which_flag_is_set:String(myFlag:Int) Local s:String = "Flags (" + myFlag + ") : " If myFlag & flag_1 s:+ " 1," If myFlag & flag_2 s:+ " 2," If myFlag & flag_4 s:+ " 4," If myFlag & flag_8 s:+ " 8," If myFlag & flag_16 s:+ " 16," If myFlag & flag_32 s:+ " 32," If myFlag & flag_64 s:+ " 64," If myFlag & flag_128 s:+ " 128," Return s End Function Print which_flag_is_set(17) + "~n" Print which_flag_is_set(102) + "~n" Print which_flag_is_set(128) + "~n" Print which_flag_is_set(255) Binary arithmetic states that when you And to bits together, the result is 1 only if both bits are 1. flag_8 = 1000 (in binary) the number 9 = 1001 (in binary). To find if flag_8 is set in your number, & them : 1000 & 1001 ------ 1000 If the result is not zero, the flag is set HTH |
| ||
Thx man! Its quite simple if you think about it. |