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..

  1. enable thursday?
  2. disable thursday?
  3. toggle thursday?
  4. enable all?
  5. 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

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -