IPhone SDK << value?

Hi, another silly simple question. I noticed that some typedefs in Apple frameworks use the characters "<<" , can someone tell me what this 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;
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: Okay, so now I understand how and why you should use the left bit-shift, my next question is: how can I check if a specific property matters, and if / then or the switch / case method?

+4
source share
5 answers

This is a way to create constants that are easy to mix. For example, you may have an API for ordering ice cream, and you can choose any of the vanilla, chocolate, and strawberry flavors. You can use booleans, but this is a bit heavy:

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

A good trick to solve this problem is to use numbers, where you can mix flavors with a simple add. Let's say 1 for vanilla, 2 for chocolate and 4 for strawberries:

 - (void) iceCreamWithFlavours: (NSUInteger) flavours; 

Now, if the number has its rightmost bit, its vanilla flavor in it, the other bit is chocolate, and the third bit on the right is strawberry. For example, vanilla + chocolate will be 1 + 2 = 3 decimal ( 011 in binary format).

The bit rate operator x << y takes the left number ( x ) and shifts its bits y times. This is a good tool for creating numerical constants:

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

Voila! Now that you need a view with a flexible left margin and a flexible right margin, you can mix flags using bitwise or: FlexibleRightMargin | FlexibleLeftMargin FlexibleRightMargin | FlexibleLeftMargin1<<2 | 1<<0 1<<2 | 1<<0100 | 001 100 | 001101 . On the receiving side, a method can mask an interesting bit using a logical and:

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

Hope this helps.

+7
source
<<means that all bits in the expression on the left side are shifted to the left by the value on the right side of the operator

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

another example: 0001 0100 <2 = 0101 0000

in most cases, a left shift is the same as multiplying by 2.
exception: when high bits are set and you shift them to the left (in a 16-bit integer 1000 0000 0000 0000 <1) they will be discarded or wrapped (I don't know how to do this in every language)

+4
source

Its a bit shift .

In C-inspired languages, left and right shift operators are "<<as well as" → ", respectively. The amount of shift space is given as the second argument to the shift operators.

+1
source

Bit shift !!!

for instance

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

+1
source

Bitwise left shift. For more information, see the Wikipedia article .

0
source

All Articles