Step 2-bit shift enumeration

Source: Internet
Author: User

Displacement enumeration

    • Displacement enumeration is a very old C-language technique
    • Bitwise AND if all is 1 the result is 1
    • Bitwise OR if all is 0 the result is 0

Drill

1 Defining enumeration types

Action Type Enumeration

typedef enum {

Actiontypetop = 1 << 0,

Actiontypebottom = 1 << 1,

Actiontypeleft = 1 << 2,

Actiontyperight = 1 << 3

} ActionType;

    • Method Target

Respond differently depending on the type of operation parameter

The type of operation can be any combination

    • Method implementation

-(void) Action: (ActionType) Type {

if (type = = 0) {

NSLog (@ "no operation");

Return

}

if (type & actiontypetop) {

NSLog (@ "Top%tu", type & Actiontypetop);

}

if (type & Actiontypebottom) {

NSLog (@ "Bottom%tu", type & Actiontypebottom);

}

if (type & Actiontypeleft) {

NSLog (@ "left%tu", type & Actiontypeleft);

}

if (type & actiontyperight) {

NSLog (@ "Right%tu", type & Actiontyperight);

}

}

    • Method invocation

ActionType type = Actiontypetop | Actiontyperight;

[Self action:type];

Code Summary

    • Use a bitwise or can set multiple types for one parameter at the same time
    • Use bitwise and can determine the specific type when executing
    • With the displacement setting, you can get a lot of combinations!
    • For a displacement enumeration type, if 0 is passed in, it means that no additional operations are done, and the execution efficiency is usually the highest
    • If in development, see the enumeration of displacements, and do not do any additional operations, parameters can be directly input 0!

IOS Unique Syntax

    • After IOS 5.0, provides a new way to define enumerations
    • Specifies the type of data in the enumeration, while defining the enumeration
    • typedef ns_options (Nsuinteger, nsjsonreadingoptions)

Displacement enumeration, you can use a bitwise OR SET value

    • typedef ns_enum (Nsinteger, Uitableviewstyle)

Numeric enumeration, setting values directly using enumerations

typedef ns_options (Nsuinteger, ActionType) {

Actiontypetop = 1 << 0,

Actiontypebottom = 1 << 1,

Actiontypeleft = 1 << 2,

Actiontyperight = 1 << 3

};

Step 2-bit shift enumeration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.