views:

23

answers:

2

Okay so I have defined my DSNavigationManager class and it has a property called DSNavigationManagerStyle managerStyle:


typedef enum {
    DSNavigationManagerStyleNone                        = 0,
    DSNavigationManagerStyleDefaultNavigationBar        = 1 << 0,
    DSNavigationManagerStyleDefaultToolBar              = 1 << 1,
    DSNavigationManagerStyleDefault                     =
        DSNavigationManagerStyleDefaultNavigationBar + 
        DSNavigationManagerStyleDefaultToolBar,
    DSNavigationManagerStyleInteractiveNavigationBar    = 1 << 2,
    DSNavigationManagerStyleInteractiveToolBar          = 1 << 3,
    DSNavigationManagerStyleInteractiveWithDarkPanel    = 1 << 4,
    DSNavigationManagerStyleInteractiveWithBackButton   = 1 << 5,
    DSNavigationManagerStyleInteractiveWithTitleBar     = 1 << 6,
    DSNavigationManagerStyleInteractiveDefault          = 
        DSNavigationManagerStyleInteractiveNavigationBar + 
        DSNavigationManagerStyleInteractiveToolBar + 
        DSNavigationManagerStyleInteractiveWithDarkPanel + 
        DSNavigationManagerStyleInteractiveWithBackButton + 
        DSNavigationManagerStyleInteractiveWithTitleBar,
} DSNavigationManagerStyle;


I just learned how to use bit-wise shifting but I don't know how to receive this information. I want to do something a little like:



DSNavigationManagerStyle managerStyle = DSNavigationManagerStyleDefault;

if(managerStyle "Has The DefaultNavigationBar bit or the DefaultToolBarBit") {
  // Implement
}
else {
    if(managerStyle "Has the InteractiveNavigationBar bit") {
        // Implement
    }
    if(managerStyle "Has the InteractiveToolBar bit") {
        // Implement
    }
    //.... and so on so that technically the object can implement all 
    //  styles, no styles, or any number of styles in between

}




Thanks in advance for any help! :)

+1  A: 
if (managerStyle & DSNavigationManagerStyleDefaultNavigationBar || managerStyle & DSNavigationManagerStyleDefaultToolBarBit) {
    // Implement
} else if (managerStyle & DSNavigationManagerStyleInteractiveNavigationBar) {
    // Implement
} else if (managerStyle & DSNavigationManagerStyleInteractiveToolBar) {
    // Implement
}
    //.... and so on so that technically the object can implement all 
    //  styles, no styles, or any number of styles in between
}

& is the bitwise AND operator. You should read the Wikipedia article on Bitwise operation.

Alexsander Akers
+1  A: 

To check for the presence of a particular bit, use the bitwise and, & (not to be confused with &&, the logical and). For example,

  01101101
& 00001000
----------
  00001000

If you use this value where it will be cast to boolean, any non-zero value is considered "true," which makes tests like this easy to read:

if (managerStyle & DSNavigationManagerStyleDefaultToolBar) {
  ...
}

But this test won't work well with your compound values - for example, anding a bitfield with DSNavigationManagerStyleDefault will return 'true' if any of its component bits are set.

If you really want to use bitfields, accustom yourself to all the bitwise operators: http://developer.apple.com/tools/mpw-tools/commandref/appc_logical.html

zem