Flag
From Unify Community Wiki
(Difference between revisions)
(Creation of page Flag) |
m (→Usage) |
||
Line 9: | Line 9: | ||
In it's current form, Flag is object designed, but it could easily be completely static. You just have to add "static" to the function and pass the flag by reference. | In it's current form, Flag is object designed, but it could easily be completely static. You just have to add "static" to the function and pass the flag by reference. | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
− | |||
f.TurnOn( 1 << 3 ); // 9 | f.TurnOn( 1 << 3 ); // 9 | ||
f.TurnOn( 1 << 5 ); // 41 | f.TurnOn( 1 << 5 ); // 41 |
Latest revision as of 04:55, 16 February 2012
Author: Berenger
Contents |
[edit] Description
Provide simple bitwise logical functions, turning a flag off and on, toggle and check.
[edit] Usage
In it's current form, Flag is object designed, but it could easily be completely static. You just have to add "static" to the function and pass the flag by reference.
f.TurnOn( 1 << 3 ); // 9 f.TurnOn( 1 << 5 ); // 41 f.TurnOff( 1 << 3 ); // 33 f.TurnOn( 1 << 2 ); // 37 f.Toggle( 1 << 2 ); // 33 f.Toggle( 1 << 2 ); // 37 print( f.Check( 1 << 5 ) ); // true print( f.Check( 1 << 6 ) ); // false
[edit] Supported Operations
Following operations are supported.
//Add the mask public int TurnOn( int mask ) //Remove the mask public int TurnOff( int mask ) //Toggle the mask public int Toggle( int mask ) //Check if mask is on public bool Check( int mask )
[edit] Installation
To be able to use Flag from all your classes (JavaScript, Boo and C#), it's best to put it into your "Standard Assets" folder.
[edit] History
- 15 February 2012
Initial release
[edit] Code (Flag.cs)
public class Flag { public Flag() : this( 0 ){} public Flag( int mask ){ m_Value = mask; } public int value{ get{ return m_Value; } } private int m_Value; //Add the mask to flags public int TurnOn( int mask ){ return m_Value |= mask; } //Remove the mask from flags public int TurnOff( int mask ){ return m_Value &= ~mask; } //Toggle the mask into flags public int Toggle( int mask ){ return m_Value ^= mask; } //Check if mask is on public bool Check( int mask ){ return ( m_Value & mask ) == mask; } }