SoFunction
Updated on 2025-03-10

How to learn iOS median operations through Objective-C enumeration.md detailed explanation

Opening

Today, when I was modifying the project, I saw the << operator (bit operation) appear in the enum. I have never known this before. This time, while the project is relatively leisurely, I took time to fully understand the operation.

bit operation

Bit operation is to perform bit-by-bit operations or shift binary numbers. It contains two operations: bit operation and shift. Let’s learn more about these two operations below.

This discusses only the operators of all bit operations in iOS. Operator symbols with the same meaning may be different in other languages.

Bit operator (the following operators are the same as Objective-C)

A bit operator contains the following:

~(inverse, unary operator): It inverses each bit of the binary of the target number

let initialBits: UInt8 = 0b00001111
let invertedBits = ~initialBits // equals 11110000

|(bitwise or): It will perform or operate on the same position of the two target numbers, rules: 0 and 0 are 0; 0 and 1 are 1; 1 and 1 are 1

let targetNum = 5 // 101
let targetNum2 = 6 // 110
print(targetNum | targetNum2) //print 7
//targetNum: 101
//targetNum2: 110
//result:  111 (Decimal 7)

&(bitwise and): It will perform an operation on the same position of the two target numbers, rules: 0 and 0 are 0; 0 and 1 are 0; 1 and 1 are 1

let targetNum = 5 // 101
let targetNum2 = 6 // 110
print(targetNum &amp; targetNum2) //print 4
//targetNum: 101
//targetNum2: 110
//result:  100 (Decimal 4)

^(exclusive OR): It will perform an exclusive OR operation on the same position of the two target numbers. If different, the bit is 1, otherwise the bit is 0. Rules: For example, 0 and 0 are 0; 0 and 1 are 1; 1 and 1 are 0

let targetNum = 5 // 101
let targetNum2 = 6 // 110
print(targetNum ^ targetNum2) //print 3
//targetNum: 101
//targetNum2: 110
//result:  011 (Decimal 3)

Shift

>> (right shift): It shifts the target number right by bit x bit

let targetNum = 5 // 101
print(targetNum &gt;&gt; 2) //print 1
//targetNum: 101
//Turn right 2 digits//result:  1 (Decimal 1)

<< (left shift): It will shift the target number bit by bit x bit left (0 on the right)

let targetNum = 5 // 101
print(targetNum &lt;&lt; 2) //print 20
//targetNum: 101
//Transfer 2 digits left//result:  10100 (Decimal 20)

Bit operations in enumeration

Through the above, we have learned about the specific calculation methods of bit operations. Let’s take a look at the specific application in enumeration.

Applications in Enumeration

Define enumeration

OC

typedef NS_OPTIONS(NSInteger, CellExLineType) {
 CellExLineTypeTopLong  = 0, 
 CellExLineTypeTopNone  = 1 &lt;&lt; 0, //Decimal 1 CellExLineTypeBottomLong = 1 &lt;&lt; 1, //Decimal 2 CellExLineTypeBottomNone = 1 &lt;&lt; 2, //Decimal 4};

Swift

struct CellExLineType: OptionSet {
 let rawValue: Int
 
 static let topLong = CellExLineType(rawValue: 0)
 static let topNone = CellExLineType(rawValue: 1 << 0)
 static let bottomLong = CellExLineType(rawValue: 1 << 1)
 static let bottomNone = CellExLineType(rawValue: 1 << 2)
}

The role of bit operations in enumeration

~(inverse): used to remove a value

//OC
 = CellExLineTypeTopNone;
 |= CellExLineTypeBottomNone;
 =  &amp; ~CellExLineTypeTopNone; // Only include CellExLineTypeBottomNone//Swift
var lineType: CellExLineType = [.topNone, .bottomNone]
(.topNone)

|(bitwise or): used to add a value

//OC
 = CellExLineTypeTopNone; // Includes CellExLineTypeTopNone =  | CellExLineTypeBottomNone; // Includes CellExLineTypeTopNone and CellExLineTypeBottomNone//Swift
var lineType: CellExLineType = [.bottomNone]
(.topNone)

&(bitwise and): used to check whether a value is included

//OC
if (( &amp; CellExLineTypeTopNone) == CellExLineTypeTopNone) {
 NSLog(@"Includes CellExLineTypeTopNone");
}
//Swift
var lineType: CellExLineType = [.topNone, .bottomNone]
if (.bottomNone) {
 print("Include bottomNone")
}

^(XOR): Used to invert a certain value (exclude if included, and add if not included)

//OC
 = CellExLineTypeTopNone | CellExLineTypeBottomNone; // Includes CellExLineTypeTopNone and CellExLineTypeBottomNone =  ^ CellExLineTypeTopNone; // Only include CellExLineTypeBottomNone =  ^ CellExLineTypeTopNone; // Includes CellExLineTypeTopNone and CellExLineTypeBottomNone//Swift
var lineType: CellExLineType = [.topNone, .bottomNone]
if (.topNone) {
 (.topNone)
} else {
 (.topNone)
}

In enumeration, we can easily assign multiple values ​​to a property value. For example, the following code assigns the CellExLineTypeTopNone and CellExLineTypeBottomNone attributes to lineType. In this way, we can handle the situations of CellExLineTypeTopNone and CellExLineTypeBottomNone in the set method of lineType.

//OC
- (void)setLineType:(CellExLineType)lineType {
 _lineType = lineType;
 if (lineType & CellExLineTypeTopNone) {
  NSLog(@"top none");
 }
 if (lineType & CellExLineTypeBottomNone) {
  NSLog(@"bottom none");
 }
}
//Swift
var lineType: CellExLineType = [.topNone, .bottomNone]
if (.topNone) {
 (.topNone)
}
if (.bottomNone) {
 
}

Many Enums in the system are also used in this way, such as UIViewAutoresizing, UIViewAnimationOptions, etc.

Why use bit operators in enums?

  • On many ancient microprocessors, bit operations are slightly faster than addition and subtraction operations, and usually bit operations are much faster than multiplication and division operations. This is not the case in modern architectures: bit operations are usually the same speed as addition operations (still faster than multiplication operations)
  • You can set multiple values ​​for a property at the same time

Summarize

  • ~(bit inverse): Inverse the target number bit inverse; used to remove a certain value in the enumeration
  • |(bitwise or): performs or operations on two target numbers at the same position; used to add a certain value in the enumeration
    • &(bitwise and): performs arithmetic operation on the two target numbers at the same position; used in the enumeration to determine whether a value is included
  • ^(bitwise exclusive OR): performs an exclusive OR operation on two target numbers at the same position; sets a certain value in the enumeration
  • >> (right shift): bitwise to the target number x bit
  • << (left shift): bitwise shift x bit left to the target number

refer to

  • Bitwise Operators and Bit Masks
  • SO
  • How to create NS_OPTIONS-style bitmask enumerations in Swift?
  • bit operation
  • Advanced Operators

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.