Using enum to define bit masks

Often you will want to define a list of constants, each representing an integer with one bit turned on. Then, these integers can be bitwise-ORed together and tested for using bitwise-AND, as described above.

The elegant way to do this is to define an enum that uses the left-shift operator to define the values. Here is how the constants for the UIDataDetector are defined:

e​n​u​m​ ​{​
 ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​P​h​o​n​e​N​u​m​b​e​r​ ​ ​ ​=​ ​1​ ​<​<​ ​0​,​
 ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​L​i​n​k​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ ​1​ ​<​<​ ​1​,​
 ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​A​d​d​r​e​s​s​ ​ ​ ​ ​ ​ ​ ​=​ ​1​ ​<​<​ ​2​,​
 ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​C​a​l​e​n​d​a​r​E​v​e​n​t​ ​=​ ​1​ ​<​<​ ​3​,​
 ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​N​o​n​e​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ ​0​,​
 ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​A​l​l​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ ​N​S​U​I​n​t​e​g​e​r​M​a​x​
}​;​
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset