In my zeal to improve as developer, I want to know my code it is possible to improve, I am new in this and have very small logical
this is my code:
private int Period(DateTime date)
{
int period = 0;
if(date!= null)
{
int numeroMes = int.Parse(date.Month.ToString());
if(numeroMes <= 2)
{
period = 1;
}
else if (numeroMes <= 4 && numeroMes > 2)
{
periodo = 2;
}
else if (numeroMes <= 6 && numeroMes > 4)
{
period = 3;
}
else if (numeroMes <= 8 && numeroMes > 6)
{
period = 4;
}
else if (numeroMes <= 10 && numeroMes > 8)
{
period = 5;
}
else if (numeroMes <= 12 && numeroMes > 8)
{
period = 6;
}
}
return period ;
}
tkns for help me.
You can use the flooring of integer division to your favour which should be faster.
private int Period(DateTime date)
{
return (date.Month + 1) / 2;
}
However rounding might be easier to understand.
private int Period(DateTime date)
{
return (int)Math.Ceiling(date.Month / 2.0);
}
You can calculate the period since every period is just two months.
As DaveShaw pointed out, there is no need to check for the date being null because DateTime is a value type and thus cannot be null.
private int Period(DateTime date)
{
// already an int, no need to convert
var month = date.Month;
if (month % 2 == 0)
return month / 2;
else
return (month + 1) / 2
}
Here's another even shorter option that A.S. suggested.
private int Period(DateTime date)
{
return (int)Math.Ceiling(date.Month / 2.0);
// or...
// return (date.Month + 1) / 2;
// but I prefer the Ceiling option since it is more obvious what is happening
}
Alternatively, you could also reduce your code in this way...
private int Period(DateTime date)
{
int month = date.Month;
if (month <= 2)
return 1;
else if (month <= 4)
return 2;
else if (month <= 6)
return 3;
else if (month <= 8)
return 4;
else if (month <= 10)
return 5;
else
return 6;
}
Since I used return statements, there is no need for the second bool in each if statement.
As others said, this would better fit in https://codereview.stackexchange.com/, since it's not a question to a problem.
Now for some improvements to your code.
if(date!= null)
This will always be true, since DateTime is a value type (struct), which can't be null. Only Nullable<DateTime>/DateTime? can be null. So you can simply remove that.
int numeroMes = int.Parse(date.Month.ToString());
Here you are converting date.Month (which is an int) to a string and then parse it back to an int. Don't do that, date.Month is an int already:
int numeroMes = date.Month;
The rest is ok so far, but i guess in this line:
else if (numeroMes <= 12 && numeroMes > 8)
you really meant numeroMes > 10. Doesn't make a difference though as values 9 and 10 are matched before that.
Finally, you could shorten all those ifs to a simple formula:
return (int)Math.Ceiling(date.Month / 2.0);
Math.Ceiling rounds up the passed decimal number to the next integral number. So 1.5 would become 2 for example.
Related
There is a method in my program that verifies if a phone number has 13 characters, I want to return false if there is more or less characters than 13, and return true if has 13 characters.
So far, i made this, i think i got the 13 characters, anything less or more it gives me an ArgumentOutOfRangeException:
public bool CheckPhone()
{
int sum = 0;
int rest;
int i;
for (i = 1; i <= 12; i++)
{
if (Phone.Substring(i - 1, i - (i - 1)).Contains("")==false)
{
return false;
}
else
{
sum += int.Parse(Phone.Substring(i - 1, i - (i - 1))) * (13 - i);
}
}
rest = (sum * 11) % 12;
if ((rest == 11) || (rest == 12))
{
rest = 0;
return true;
}
else
{
return false;
}
}
public bool CheckPhone()
=> Phone.Length == 13 && Phone.All(Char.IsDigit);
I'm porting some calculations from Excel to C# which use the Days360 function (the default/US method). Using the Wikipedia page as a guide, I came up with this code:
public static int Days360(DateTime a, DateTime b)
{
var dayA = a.Day;
var dayB = b.Day;
if (IsLastDayOfFebruary(a) && IsLastDayOfFebruary(b))
dayB = 30;
if (dayA == 31 || IsLastDayOfFebruary(a))
dayA = 30;
if (dayA == 30 && dayB == 31)
dayB = 30;
return ((b.Year - a.Year) * 12 + b.Month - a.Month) * 30 + dayB - dayA;
}
private static bool IsLastDayOfFebruary(DateTime date)
{
if (date.Month != 2)
return false;
int lastDay = DateTime.DaysInMonth(date.Year, 2);
return date.Day == lastDay;
}
I tested it with a (small) range of inputs and the results mostly agree with Excel's native function except if I use 2015-02-28 for both a and b. My code returns 0 and Excel -2.
My result seems more reasonable but at this point, I'd prefer to calculate the exact same result as Excel. There might be other inputs where they disagree so I don't want to make a special case just for that date.
Does anyone know the exact algorithm that Excel uses?
EDIT: There was a glaring bug in the original code I posted which is unrelated to the question. I had already fixed that one but I copied from the wrong file when posting the question.
According to this Wikipedia article the Microsoft Excel Days360 function is equivalent to 30/360 BMA/PSA. So to get exact results as MS Excel we need to implement the BMA/PSA method. I have implemented the method.
private double Days360(DateTime StartDate, DateTime EndDate)
{
int StartDay = StartDate.Day;
int StartMonth = StartDate.Month;
int StartYear = StartDate.Year;
int EndDay = EndDate.Day;
int EndMonth = EndDate.Month;
int EndYear = EndDate.Year;
if (StartDay == 31 || IsLastDayOfFebruary(StartDate))
{
StartDay = 30;
}
if (StartDay == 30 && EndDay == 31)
{
EndDay = 30;
}
return ((EndYear - StartYear) * 360) + ((EndMonth - StartMonth) * 30) + (EndDay - StartDay);
}
private bool IsLastDayOfFebruary(DateTime date)
{
return date.Month == 2 && date.Day == DateTime.DaysInMonth(date.Year, date.Month);
}
I had the same need, I found the solution in the function on line 51 of this phpexcel library
dateDiff360
this is part of the class code for the calculation
/**
* Identify if a year is a leap year or not
*
* #param integer $year The year to test
* #return boolean TRUE if the year is a leap year, otherwise FALSE
*/
public static function isLeapYear($year)
{
return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
}
/**
* Return the number of days between two dates based on a 360 day calendar
*
* #param integer $startDay Day of month of the start date
* #param integer $startMonth Month of the start date
* #param integer $startYear Year of the start date
* #param integer $endDay Day of month of the start date
* #param integer $endMonth Month of the start date
* #param integer $endYear Year of the start date
* #param boolean $methodUS Whether to use the US method or the European method of calculation
* #return integer Number of days between the start date and the end date
*/
private static function dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS)
{
if ($startDay == 31) {
--$startDay;
} elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::isLeapYear($startYear))))) {
$startDay = 30;
}
if ($endDay == 31) {
if ($methodUS && $startDay != 30) {
$endDay = 1;
if ($endMonth == 12) {
++$endYear;
$endMonth = 1;
} else {
++$endMonth;
}
} else {
$endDay = 30;
}
}
return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
}
This algorithm also include the optional parameter method:
int startMonthDays = 0;
int endMonthDays = 0;
double diff = 0;
if(method.Equals("TRUE"))
{
if(dtStartDate.getDay() < 30)
{
startMonthDays = (30 - dtStartDate.getDay());
}
else
{
startMonthDays = 0;
}
if(dtEndDate.getDay() < 30)
{
endMonthDays = dtEndDate.getDay();
}
else
{
endMonthDays = 30;
}
diff = (dtEndDate.getYear() - dtStartDate.getYear())*360 +
(dtEndDate.getMonth() - dtStartDate.getMonth() - 1)*30 +
startMonthDays + endMonthDays;
}
else
{
if(DateCalendar.daysInMonth(dtStartDate.getYear(), dtStartDate.getMonth()) == dtStartDate.getDay())
{
startMonthDays = 0;
}
else
{
startMonthDays = (30 - dtStartDate.getDay());
}
if(DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) == dtEndDate.getDay())
{
if(dtStartDate.getDay() < DateCalendar.daysInMonth(dtStartDate.getYear(), dtStartDate.getMonth()) - 1)
{
if(DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) > 30)
{
endMonthDays = DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth());
}
else
{
endMonthDays = dtEndDate.getDay();
}
}
else
{
if(DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) > 30)
{
endMonthDays = DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) - 1;
}
else
{
endMonthDays = dtEndDate.getDay();
}
}
}
else
{
endMonthDays = dtEndDate.getDay();
}
diff = (dtEndDate.getYear() - dtStartDate.getYear())*360 +
(dtEndDate.getMonth() - dtStartDate.getMonth() - 1)*30 +
startMonthDays + endMonthDays;
}
with
public static int daysInMonth (int year, int month)
{
if (DateTime.IsLeapYear(year) && month == 2)
{
return 29;
}
else
{
return table[month - 1];
}
}
and
private static readonly int[] table = new int[]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Test the next
public static int Days360(DateTime a, DateTime b)
{
var dayA = a.Day;
var dayB = b.Day;
if (IsLastDayOfMonth(a) && IsLastDayOfMonth(b)) {
dayB = Math.min(30, dayB);
} else if (dayA == 30 && dayB ==31) {
DayB = 30;
}
if (IsLastDayOfMonth(a))
dayA = 30;
return ((b.Year - a.Year) * 360 + b.Month - a.Month) * 30 + dayB - dayA;
}
private static bool IsLastDayOfMonth(DateTime date)
{
int lastDay = DateTime.DaysInMonth(date.Year, date.Month);
return date.Day == lastDay;
}
if ((DateTime.Now.DayOfWeek != DayOfWeek.Friday && DateTime.Now.DayOfWeek != DayOfWeek.Saturday) &&
((DateTime.Now.Hour >= 10 && DateTime.Now.Hour < 13) || (DateTime.Now.Hour >= 20 && DateTime.Now.Hour < 23)))
I have to shorten this condition, any suggestions?
You could change the hours to use
(DateTime.Now.Hour % 12) +1 >= 10 && (DateTime.Now.Hour % 12) +1 < 13
Maybe even without the second check.
I don't think you can improve much more than that than looking for other methods like other answers
Update
I tested the above and its wrong, but this is more sadistic and works
var check = (DateTime.Now.Hours - 10 % 12) % 10;
var checkV = (DateTime.Now.Hours >= 10 && check < 3);
Test Code
for (int i = 0; i < 24; i++)
{
var check = (i - 10 % 12) % 10;
bool checkV = (i >= 10 && check < 3);
Console.WriteLine(i.ToString() + ": " + checkV.ToString());
}
Console.ReadKey();
Update 2
Complete shortened code
if( (int)DateTime.Now.DayOfWeek < 5 &&
DateTime.Now.Hours >= 10 &&
((DateTime.Now.Hours - 10 % 12) % 10) < 3)
Well, you could build an extension method:
public static bool BoundsCheck(this DateTime d, int min, int max, int min2, int max2)
{
return (d.DayOfWeek != DayOfWeek.Friday &&
d.DayOfWeek != DayOfWeek.Saturday &&
d.Hour >= min &&
d.Hour < max) ||
(d.Hour >= min2 && d.Hour < max2);
}
and then call it like this:
if (DateTime.Now.BoundsCheck(10, 13, 20, 23))...
Is this shorter? Maybe, but more important in my opinion it's more readable and maintainable:
var now = DateTime.Now;
var notAllowedDays = new[] { DayOfWeek.Friday, DayOfWeek.Saturday };
var allowedHours = Enumerable.Range(10, 3).Concat(Enumerable.Range(20, 3));
if(!notAllowedDays.Contains(now.DayOfWeek) && allowedHours.Contains(now.Hour))
{
}
if (!this.ItsPartyDay() && (this.ItsLunchTime() || this.ItsDinnerTime()))
{
...
}
private bool ItsPartyDay()
{
return (Int32)DateTime.Now.DayOfWeek >= 5;
}
private bool ItsLunchTime()
{
return (DateTime.Now.Hour >= 10 && DateTime.Now.Hour < 13);
}
private bool ItsDinnerTime()
{
return (DateTime.Now.Hour >= 20 && DateTime.Now.Hour < 23);
}
I don't think there is any reasonable solution but here a couple that come to mind. Use aliases for DateTime and DayOfWeek. One other option would be to assign all of those values to variables before the conditional.
So you could do things like;
string fri = DayOfWeek.Friday;
string sat = DayOfWeek.Saturday;
then use those in the conditional. Or;
using dt = DateTime;
Then you could do dt.Now.DayOfWeek
I personally would not recommend doing either of these things. You're not actually shortening the conditional, you're just refactoring. If you have a lot of these in one class it might be worth the trade off, otherwise it's probably not.
EDIT: The extension method suggestion by Michael Perrenoud is a reasonable solution that actually works really well.
I want to calculate the count of total quarters (of a year) in the given time span.
for example:
start date = 1-june -2009
end date = 18-july-2011
count should be = 10.
one more
start date = 4-Jan-2009
end date = 27-oct -2010
count =8.
I have not been able to get the correct result.
Your example is wrong: there are only 7 quarters between 4-Jan-2009 and 27-oct -2010
You could simply add a reference to the Microsoft.VisualBasic.dll to your project and use DateDiff:
VB:
Public Shared Function getQuartersBetween(ByVal d1 As Date, ByVal d2 As Date) As Int32
Return DateDiff(DateInterval.Quarter, d1, d2)
End Function
C#:
public static int getQuartersBetween(System.DateTime d1, System.DateTime d2)
{
return Microsoft.VisualBasic.DateAndTime.DateDiff(DateInterval.Quarter, d1, d2);
}
or you could write your own implementation:
public class Quarter
{
public static long GetQuarters(DateTime dt1, DateTime dt2)
{
double d1Quarter = GetQuarter(dt1.Month);
double d2Quarter = GetQuarter(dt2.Month);
double d1 = d2Quarter - d1Quarter;
double d2 = (4 * (dt2.Year - dt1.Year));
return Round(d1 + d2);
}
private static int GetQuarter(int nMonth)
{
if (nMonth <= 3)
return 1;
if (nMonth <= 6)
return 2;
if (nMonth <= 9)
return 3;
return 4;
}
private static long Round(double dVal)
{
if (dVal >= 0)
return (long)Math.Floor(dVal);
return (long)Math.Ceiling(dVal);
}
}
or in VB.NET:
Public Class Quarter
Public Shared Function GetQuarters(ByVal dt1 As DateTime, ByVal dt2 As DateTime) As Long
Dim d1Quarter As Double = GetQuarter(dt1.Month)
Dim d2Quarter As Double = GetQuarter(dt2.Month)
Dim d1 As Double = d2Quarter - d1Quarter
Dim d2 As Double = (4 * (dt2.Year - dt1.Year))
Return Round(d1 + d2)
End Function
Private Shared Function GetQuarter(ByVal nMonth As Integer) As Integer
If nMonth <= 3 Then
Return 1
End If
If nMonth <= 6 Then
Return 2
End If
If nMonth <= 9 Then
Return 3
End If
Return 4
End Function
Private Shared Function Round(ByVal dVal As Double) As Long
If dVal >= 0 Then
Return CLng(Math.Floor(dVal))
End If
Return CLng(Math.Ceiling(dVal))
End Function
End Class
Code for you : Try below code
public static void Main()
{
//Application.Run(new XmlTreeDisplay());
int monthdiuff = monthDifference(Convert.ToDateTime("01/04/09"), Convert.ToDateTime("10/27/10"));
Console.WriteLine(monthdiuff);
int totalQuater = (monthdiuff / 3) + (monthdiuff%3);
Console.WriteLine(totalQuater);
Console.ReadLine();
}
private static int monthDifference(DateTime startDate, DateTime endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}
Without some code to look over I can't help you find your exact problem.
If it were me I would probably find the difference between the dates in days, then divide by number of days in a quarter (91 or so). I'm sure that C# has some kind of date parsing module that can read in the dates as a string, giving you two objects that you could then subtract to find the difference in days.
This is one crude form of calculating the quarters based on your assumptions, you can choose to modify as it is it works good enough
DateTime dt1 = new DateTime(2009, 1, 1);// new DateTime(2009, 6, 1);
DateTime dt2 = new DateTime(2010, 10, 27);// new DateTime(2011, 7, 18);
if (dt1.Month < 4)
dt1 = new DateTime(dt1.Year,1,1);
else if (dt1.Month < 7)
dt1 = new DateTime(dt1.Year,4,1);
else if (dt1.Month < 10)
dt1 = new DateTime(dt1.Year,7,1);
else
dt1 = new DateTime(dt1.Year,10,1);
if (dt2.Month < 4)
dt2 = new DateTime(dt2.Year, 3, DateTime.DaysInMonth(dt2.Year, 3));
else if (dt2.Month < 7)
dt2 = new DateTime(dt2.Year, 6, DateTime.DaysInMonth(dt2.Year, 6));
else if (dt2.Month < 10)
dt2 = new DateTime(dt2.Year, 9, DateTime.DaysInMonth(dt2.Year, 9));
else
dt2 = new DateTime(dt2.Year, 12, DateTime.DaysInMonth(dt2.Year, 12));
TimeSpan ts = dt2 - dt1;
int quarters = (int) ts.TotalDays/90;
Console.WriteLine(quarters);
I am baselining the dates to the start and end of the quarters as you want and then assuming for 90 day quarter transforming the diff as int. Works for your mentioned examples,see if it suits you well enough
If the definition of a quarter is a 90-day difference, it's easy of course:
internal static int GetNumberOfQuarters(DateTime p_DtStart, DateTime p_DtEnd)
{
TimeSpan span = p_DtEnd.Subtract(p_DtStart);
return (int)span.TotalDays % 90;
}
But that's not what you're looking for. What about this (not tested but you'll get the idea)
internal static class DateTimeTools
{
internal static int GetNumberOfQuartersBetweenDates(DateTime startDate, DateTime endDate)
{
int iYearStart, iYearEnd, iMonthStart, iMonthEnd, iDayStart, iDayEnd;
iYearStart = startDate.Year;
iYearEnd = endDate.Year;
iMonthStart = startDate.Month;
iMonthEnd = endDate.Month;
iDayStart = startDate.Day;
iDayEnd = endDate.Day;
int iYearDiff, iQuarterDiff, iDayDiff;
iYearDiff = iYearEnd - iYearStart;
iQuarterDiff = iMonthEnd % 3 - iMonthStart % 3;
iDayDiff = iDayEnd - iDayStart;
int iNumOfQuarters = 0;
// at least a year difference?
if ((iYearDiff > 0 && iQuarterDiff > 0) || iYearDiff > 0 && iQuarterDiff == 0 && iDayDiff >= 0)
{
iNumOfQuarters = iYearDiff * 4 + iQuarterDiff;
}
// at least a quarter difference?
// within different years
if ((iYearDiff > 0 && iQuarterDiff <= 0)) // eg, dec 2010 - feb 2011 iYearDiff 1 iQuarterDiff -3
{
if ((iQuarterDiff == -3 && iDayDiff >= 0) || iQuarterDiff > -3)
{
iNumOfQuarters = iQuarterDiff + 4;
}
}
// within the same year
if (iYearDiff == 0 && iQuarterDiff > 0)
{
if ((iQuarterDiff == 1 && iDayDiff >= 0) || iQuarterDiff > 1)
{
iNumOfQuarters = iQuarterDiff;
}
}
return iNumOfQuarters;
}
}
Regards,
Nico
public static string GetQuarter(this DateTime date)
{
var quarterList = new List<string>();
if (date.Month >= 1 && date.Month <= 3)
return "Q1";
else if (date.Month >= 4 && date.Month <= 6)
return "Q1,Q2";
else if (date.Month >= 7 && date.Month <= 9)
return "Q1,Q2,Q3";
else
return "Q1,Q2,Q3,Q4";
}
This too can be used as an extension method if you are expecting to get a list of Quarters, You can later on use GetQuarter().Split(new[] { ',' }).Count() to get the count.
Easy formula to get quarters difference:
{
int firstQuarter = getQuarter(first);
int secondQuarter = getQuarter(second);
return 1 + Math.Abs(firstQuarter - secondQuarter);
}
private static int getQuarter(DateTime date)
{
return (date.Year * 4) + ((date.Month - 1) / 3);
}
HI there, I'm slightly new to programming, more of a hobby. I am wondering if a the following logic or technique has a specific name, or term. My current project has 7 check boxes, one for each day of the week. I needed an easy to save which boxes were checked.
The following is the method to saved the checked boxes to a single number. Each checkbox gets a value that is double from the last check box. When I want to find out which boxes are checked, I work backwards, and see how many times I can divide the total value by the checkbox value.
private int SetSelectedDays()
{
int selectedDays = 0;
selectedDays += (dayMon.Checked) ? 1 : 0;
selectedDays += (dayTue.Checked) ? 2 : 0;
selectedDays += (dayWed.Checked) ? 4 : 0;
selectedDays += (dayThu.Checked) ? 8 : 0;
selectedDays += (dayFri.Checked) ? 16 : 0;
selectedDays += (daySat.Checked) ? 32 : 0;
selectedDays += (daySun.Checked) ? 64 : 0;
return selectedDays;
}
private void SelectedDays(int n)
{
if ((n / 64 >= 1) & !(n / 64 >= 2))
{
n -= 64;
daySun.Checked = true;
}
if ((n / 32 >= 1) & !(n / 32 >= 2))
{
n -= 32;
daySat.Checked = true;
}
if ((n / 16 >= 1) & !(n / 16 >= 2))
{
n -= 16;
dayFri.Checked = true;
}
if ((n / 8 >= 1) & !(n / 8 >= 2))
{
n -= 8;
dayThu.Checked = true;
}
if ((n / 4 >= 1) & !(n / 4 >= 2))
{
n -= 4;
dayWed.Checked = true;
}
if ((n / 2 >= 1) & !(n / 2 >= 2))
{
n -= 2;
dayTue.Checked = true;
}
if ((n / 1 >= 1) & !(n / 1 >= 2))
{
n -= 1;
dayMon.Checked = true;
}
if (n > 0)
{
//log event
}
}
The method works well for what I need it for, however, if you do see another way of doing this, or a better way to writing, I would be interested in your suggestions.
Someone else mentioned bit masking, but I thought I would show you a way to simplify your code.
daySun.Checked = (n & 64) == 64;
daySat.Checked = (n & 32) == 32;
dayFri.Checked = (n & 16) == 16;
dayThu.Checked = (n & 8) == 8;
dayWed.Checked = (n & 4) == 4;
dayTue.Checked = (n & 2) == 2;
dayMon.Checked = (n & 1) == 1;
This resembles bitmasking. When I can find the blog I read this week using this exact example I'll post it!
Ah got it! Here it is.
You can then do things like:
DaysOfWeek week = DaysOfWeek.Sunday | DaysOfWeek.Monday;
to select Sunday and Monday. Or in your example, when you check the value of each checkbox you can do:
DaysOfWeek week = DaysOfWeek.None; // DaysOfWeek.None = 0
if (Monday.Checked)
{
week |= DaysOfWeek.Monday;
}
and to check if a particular day is set:
DaysOfWeek week = DaysOfWeek.Monday | DaysOfWeek.Tuesday;
// this will be FALSE (so Wednesday will remain unchecked) because "week" contains Monday/Tuesday, but not Wednesday.
if ((week & DaysOfWeek.Wednesday) == DaysOfWeek.Wednesday)
{
Wednesday.Checked = true;
}
EDIT:
.NET's built-in DayOfWeek does not allow for bitmasking multiple values, so you'll need to roll your own DaysOfWeek enum.
You could create an enum with all days and mark it with the attribute [Flags] then give each day the same value as your (bla.checked) ? XX..
then you could use +=, and, or to get the same functionality..
so to check if a value contains lets say monday you would do
if (myEnum & Days.Monday == Days.Monday)
{
...
}
It is called bitmasking and you can do the same thing more easily using an Enum with the Flags attribute.
It's called a bitfield, and yes, it's the most space-efficient way to solve this. Using separate booleans will probably use more memory, but IMO the better readability is worth six bytes or so.
you can use an enum... its more readable and pretty
public enum daysOfWeek
{
Mon = 1, Tue = 2, Wed = 4, Thu = 8, Fri = 16, Sat = 32, Sun = 64
}
you can hide the complexity in a function:
and it's better to bit-shift instead of dividing
private bool get_bit(int val, int idx)
{
return ((val >> idx) & 1) != 0;
}