Objective-c using bit arithmetic to design an enumeration that can be checked

Source: Internet
Author: User

To design an enumeration that can be checked by using bit operationsOne Small example of enumeration use

In software development, enumerations are a way of programming that we will often use, with enumerations that make our code more readable and unified. Typically, we use a typedef to define a type of enumeration. For example:

typedef enum {PARA1, para2, Para3}myenum;

We can use it in the parameters of the function:

-(void) TESTEUNM: (myenum) para{    switch  (para)  {         case para1:        {             nslog (@ "para%d", 1);         }            break;         case para2:        {              nslog (@ "para%d", 2);         }            break;         case para3:        {              nslog (@ "para%d", 3);        &nBSP;}             break;         default:            break;     }}

When we call such a function, we can shunt the action we want by passing in an enumeration type.

second, what exactly is an enumeration?

Enumeration is not a strange type, the essence is another name of the integer type, if there is no additional operation, the first parameter in the enumeration is the integer type 0, followed by sliding scale, of course, we can also be considered to control this value, for example:

typedef enum {para1=7, para2, Para3=1000}myenum;

As you can see, when I pass the argument, 7 and para1 are exactly the same, but the latter is significantly more readable.

Three, the enumeration properties that can be checked

If you are familiar with the development of iOS, you will find that many of the enumeration types of the system can be checked, such as the stretching mode of the view, the parsing properties of JSON, and so on, which not only optimizes the readability of the code, but also simplifies the code of some option settings. When we understand that enumerations are integral types, we find it easy to do so:

First, we set a regular value for the enumeration parameter that is defined:

typedef enum {para1=1<<1, para2=1<<2, para3=1<<3}myenum;

The << symbol is the left-shift operator in bitwise operations, and after the left shift of 1 1-bit, 2-bit, 3-bit, we get the following binary numbers:

000100100100

Now we have a general idea, with the current bit of 0 and one to identify whether the current property is set, if there are several properties of the check, just need to make our corresponding enumeration or the bitwise operation, when fetching the corresponding bit is 1, which is what the operation can do:

-(void) TESTEUNM: (myenum) para{if (para&1<<1) {NSLog (@ "para1");    } if (para&1<<2) {NSLog (@ "Para2");    } if (para&1<<3) {NSLog (@ "Para3"); }}

We call it by the following way:

[Self testeunm:para2|para3];

The printing results are as follows:

This enumeration of bit operations is widely used in the official SDK to simplify the code and improve the readability of the code effectively.



Objective-c using bit arithmetic to design an enumeration that can be checked

Related Article

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.