iphone - Some questions about bitwise operators -
i read bitwise operators today , seem rather handy me. noticed apple uses them too, example uiviewautoresizing
.
in app need keep track of 7 days of week. each day can either enabled or disabled. used have 7 bool
's, i'm trying use single integer this:
enum { daysmonday = 1 << 0, daystuesday = 1 << 1, dayswednesday = 1 << 2, daysthursday = 1 << 3, daysfriday = 1 << 4, dayssaturday = 1 << 5, dayssunday = 1 << 6 }; typedef nsuinteger days;
my question is, how can enable/disable values now? know can check variable days
specific day this:
if (days & daysthursday) { // thursday enabled }
but how i..
- enable thursday?
- disable thursday?
- toggle thursday?
- enable all?
- disable all?
thank you.
i'm not familiar objective-c, here basics when dealing bitwise operators.
enable thursday
days = days | daysthursday;
disable thursday
alldays = daysmonday | daystuesday | ... | dayssunday; days = days & (alldays ^ daysthursday); // or days = days & ~daysthursday;
toggle thursday
days = days ^ daysthursday;
enable all
alldays = daysmonday | daystuesday | ... | dayssunday; days = days | alldays; // or days = alldays;
disable all
days = days ^ days; // or days = 0;
Comments
Post a Comment