Xiaogu iOS low-level blog collection
-
When exploring the bottom of iOS, there isa programmer is not unfamiliar with isa, isa implementation uses the union and bitfield
-
In fact, there is basically no writing union and bitfield in iOS development, but it does not mean that we can not ah. I think it is good to reach the point of understanding!!
1. A consortium
- There’s a structure type that I use a lot
strut
There is a structure that is similar to a structure calledThe appropriate
, also known asA consortium
.
Union Common body name {member list}Copy the code
A consortium
withThe structure of the body
The difference between:
Each member of the structure has a separate block of memory, and does not affect each other! And all members of the consortium share one memory! Pull one and move the whole body!
As a programmer, or code is more intuitive!!
1.1 Characteristics of the Consortium 1
union XGXS { int a; //4 bytes short b; //2 bytes char c; //1 byte} XGXS; XGXS. A = 2; xgxs.b = 4; xgxs.c = 'c'; NSLog(@"union size: %lu - %lu",sizeof(xgxs), sizeof(union XGXS)); //union size: 4 - 4Copy the code
Conclusion 1: The memory of the union is the maximum memory required by the member. 民运分子
1.2 Characteristics of the Consortium 2
xgxs.a = 2;
NSLog(@"%d---%d---%c",xgxs.a,xgxs.b,xgxs.c);/ / 2-2 -
xgxs.b = 4;
NSLog(@"%d---%d---%c",xgxs.a,xgxs.b,xgxs.c);/ / 4-4 -
xgxs.c = 'c';
NSLog(@"%d---%d---%c",xgxs.a,xgxs.b,xgxs.c);//99---99---c
Copy the code
** draws the conclusion 2: every time a member of the consortium is changed, other members will be affected, that is, the members of the consortium are mutually exclusive **
That’s all you need to know for the union!!
2. A domain
2.1 Use bool to prove
- Bit field words or better to understand!
- For example: Suppose I had a tractor! The tractor can only go forward, backward, left, right, (
You can't look at the sky at 45 degrees
)
@interface XGTractor : NSObject
@property (nonatomic, assign) BOOL front;
@property (nonatomic, assign) BOOL back;
@property (nonatomic, assign) BOOL left;
@property (nonatomic, assign) BOOL right;
@end
@implementation XGTractor
@end
XGTractor *tractor = [[XGTractor alloc] init];
tractor.back = YES;
tractor.front = YES;
tractor.left = YES;
tractor.right = YES;
Copy the code
- We print this tractor
-
We can see that front, back, left, and right are each one byte, but we only need one binary to represent them, i.e. 0 or 1!
-
At this point we can use bitfields to look at it
2.2 Use bit-field proof
In the code
@interface XGTractor2 : NSObject - (void)setFront:(BOOL)isFront; - (BOOL)isFront; - (void)setBack:(BOOL)isBack; - (BOOL)isBack; - (void)setLeft:(BOOL)isLeft; - (BOOL)isLeft; - (void)setRight:(BOOL)isRight; - (BOOL)isRight; @end #define XGDirectionFrontMask (1 << 0) #define XGDirectionBackMask (1 << 1) #define XGDirectionLeftMask (1 << 2) #define XGDirectionRightMask (1 << 3) @interface XGTractor2 () { union { char bits; struct { char front: 1; char back: 1; char left: 1; char right: 1; }; } _direction; } @end @implementation XGTractor2 - (instancetype)init { self = [super init]; if (self) { _direction.bits = 0b00000000; } return self; } - (void)setFront:(BOOL)isFront { _direction.front = isFront; } - (BOOL)isFront { return !! (_direction.bits & XGDirectionFrontMask); } - (void)setBack:(BOOL)isBack { _direction.back = isBack; } - (BOOL)isBack { return !! (_direction.back & XGDirectionBackMask); } - (void)setLeft:(BOOL)isLeft { _direction.left = isLeft; } - (BOOL)isLeft { return !! (_direction.left & XGDirectionLeftMask); } - (void)setRight:(BOOL)isRight { _direction.right = isRight; } - (BOOL)isRight { return !! (_direction.left & XGDirectionLeftMask); } @endCopy the code
XGTractor2 *tr2 = [[XGTractor2 alloc] init];
tr2.front = 1;
tr2.back = 1;
tr2.left = 1;
tr2.right = 1;
Copy the code
The results:
I’ve drawn the following diagram:
Based on this structure is the bitfield