Bitwise operations


Page 168… Bitwise operators and binary numbers.

I understand the binary number system. But I don’t see how to use this information with Objective-C. Can anyone hint to why this information is presented here? It seems it would be in the “For the More Curious” section.


The autoresize mask of a view is a bitmask. So, you have to understand bitwise operations, like & and |, to flip the switches of a bitmask.


I’m not quite getting this. From comparing the

[view setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight];

with fig 9.8, it seems to indicate that the default masks are all struts enabled (fixed left/right/bottom/top margins) but with no springs enabled. Is that right? Also I’m not understanding setting these programatically interacts with settings made for that object in the UI.


That particular mask says that the vertical spring is on, and the left strut is off. Therefore, the horizontal spring is off and the top/right/bottom struts are on.


Yeah it’s kinda confusing because the code describes a margin or the view being flexible (and resizing) versus the XIB UI which uses the outside struts to indicate that the margins are fixed!

I’m still confused how the XIB and setting it in code interact but I guess you would probably set this in the XIB rather than in code anyway.


I think page 168 may just be the hardest page in the book so far for me but I may have just got it.

We use a bitwise-OR operator to set the autoresizing mask and a bitwise-AND operator to check the autoresizing mask.

Is that right?


I’m trying to understand the following:

// Return YES if incoming orientation is portrait
// or either of the Landscapes, otherwise, return NO
return (x == UIInterfaceOrientationPortrait) || UIInterfaceOrientationIsLandscape(x);

but just can’t work it out. If anyone could explain that final line to me I’d be v grateful. I understand from the comments what it does, but not how / why it does it.



If you look up UIInterfaceOrientationIsLandscape in the docs, you’ll see it’s a UIKit function:

#define UIDeviceOrientationIsLandscape(orientation) \ ((orientation) == UIDeviceOrientationLandscapeLeft || \ (orientation) == UIDeviceOrientationLandscapeRight)

So it’s just a shortcut to support both landscape orientations and that line could be rewritten as


Thank you so much, that’s really helpful.