views:

93

answers:

5

Hi another silly simple question. I have noticed that in certain typedefs in Apple's frameworks use the symbols "<<" can anyone tell me what that means?:


enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;


Edit: Alright so I now understand how and why you would use the left bit-shift, my next question is how would I test to see if the the value had a certain trait using and if/then statement or a switch/case method?

A: 

Bitwise shift left. For more info see the Wikipedia article.

Steam Trout
Im not sure what that means. Could you explain?
Dick Savagewood
More links with explanations (updated the original answer with Wikipedia link):http://msdn.microsoft.com/en-us/library/f96c63ed.aspxhttp://www.cprogramming.com/tutorial/bitwise_operators.html
Steam Trout
+1  A: 

Its a bit shift.

In C-inspired languages, the left and right shift operators are "<<" and ">>", respectively. The number of places to shift is given as the second argument to the shift operators.

Russell Dias
+4  A: 

The << means that all bits in the expression on the left side are shifted left by the amount on the right side of the operator

so 1 << 1 means: 0001 becomes 0010 (those are binary numbers)

another example: 0001 0100 << 2 = 0101 0000

most of the time shift left is the same as multiply by 2.
exception: when high bits are set and you shift them left (in a 16 bit integer 1000 0000 0000 0000 << 1) they will be discarded or wrapped around (i don't know how it is done in each language)

peter
+4  A: 

This is a way to create constants that would be easy to mix. For example you can have an API to order an ice cream and you can choose any of vanilla, chocolate and strawberry flavours. You could use booleans, but that’s a bit heavy:

- (void) iceCreamWithVanilla: (BOOL) v chocolate: (BOOL) ch strawerry: (BOOL) st;

A nice trick to solve this is using numbers, where you can mix the flavours using simple adding. Let’s say 1 for vanilla, 2 for chocolate and 4 for strawberry:

- (void) iceCreamWithFlavours: (NSUInteger) flavours;

Now if the number has its rightmost bit set, it’s got vanilla flavour in it, another bit stands for chocolate and the third bit from right is strawberry. For example vanilla + chocolate would be 1+2=3 decimal (011 in binary).

The bitshift operator x << y takes the left number (x) and shifts its bits y times. It’s a good tool to create numeric constants:

1 << 0 = 001 // vanilla
1 << 1 = 010 // chocolate
1 << 2 = 100 // strawberry

Voila! Now when you want a view with flexible left margin and flexible right margin, you can mix the flags using bitwise or: FlexibleRightMargin | FlexibleLeftMargin1<<2 | 1<<0100 | 001101. On the receiving end the method can mask the interesting bit using logical and:

// 101 & 100 = 100 or 4 decimal, which boolifies as YES
BOOL flexiRight = givenNumber & FlexibleRightMargin;

Hope that helps.

zoul
A: 

Bit Shift!!!

For example

500 >> 4 = 31,

Original: 111110100
1st Shift:011111010
2nd Shift:001111101
3rd Shift:000111110
4th Shift:000011111 which equals 31.

Same as

500/16 = 31

500/2^4 = 31

Mark