Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

bitmasking in c#

[Flags]
public enum Days
{
    None = 0,	//must have a specified 0
    Sunday = 1 << 0,	//1
    Monday = 1 << 1,	//2
    Tuesday = 1 << 2,	//4
    Wednesday = 1 << 3,	//8
    Thursday = 1 << 4,	//16
    Friday = 1 << 5,	//32
    Saturday = 1 << 6,	//64
    
    Workdays = Monday | Tuesday | Wednesday | Thursday | Friday,	// 0111110
    Vacationdays = Saturday | Sunday,								// 1000001
    AllDays = Workdays | Vacationdays								// 1111111
}

//Example:
public Days myDays = Days.Wednesday | Days.Friday;	//myDays = 0101000
public bool containsTuesday = (myDays & Days.Tuesday) == Days.Tuesday ? true : false; // 0101000 & 0000100 = 0000000 (not 0000100, so expression is FALSE)
Console.WriteLine(containsTuesday); //output: false

//Operator cheat-sheet:
// & - apply mask
// | - combine
// ^ - combine/toggle, get difference
Comment

bitmasking in c#


[Flags]
public enum Names
{
    None = 0,
    Susan = 1,
    Bob = 2,
    Karen = 4
}

Comment

PREVIOUS NEXT
Code Example
Csharp :: ALWAYS MAximize window on start c# 
Csharp :: Check object is in layermask unity 
Csharp :: textmesh pro text unity 
Csharp :: how to run an external program with c# 
Csharp :: get mouse position unity 
Csharp :: c# for each textbox lines 
Csharp :: Getting data from selected datagridview row and which event 
Csharp :: how to change image color unity 
Csharp :: c# player movement 
Csharp :: c sharp list of strings 
Csharp :: unity how to copy something to the clipboard 
Csharp :: c# get full URL of page 
Csharp :: wann war der dritte weltkrieg 
Csharp :: how to move a object in unity c# 
Csharp :: how to get the directory of the project in c# 
Csharp :: add two numbers in c# 
Csharp :: exit application wpf 
Csharp :: sconvert string to title case + C3 
Csharp :: c# length 2d array 
Csharp :: instantiate an object at a certain position unity 
Csharp :: c# prime factorization 
Csharp :: c# monogame mouse position 
Csharp :: remap float c# 
Csharp :: unity how to add force 
Csharp :: messagebox.show c# error 
Csharp :: unity open website url 
Csharp :: Base64String to stream c# 
Csharp :: unity rotate vector around point 
Csharp :: add tablelayoutpanel dynamicly to winform in c# 
Csharp :: c# console writeline array 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =