设为首页 加入收藏

TOP

位域-isa指针(二)
2019-10-09 20:06:23 】 浏览:110
Tags:位域 -isa 指针

2019-10-08 18:22:37.051464+0800 SetAndGetsForMask[1966:248996] 0,10,7
0,2,7
 Program ended with exit code: 0

//分析

1)unsigned即无符号整型,占4个字节;结构体中成员变量所占内存相互独立且连续;

2)以a为例,所占位数为9位即0b111111111(十进制511),所以a的取值范围0~511,如果是512(二进制0b1000000000),由于只取低9位(000000000),所以取出值为0;

3)按位与&:两个都为1运算结果为1,否则为0;按位或|:两个都为0运算结果为0,否则为1;

2.参照isa,共用体套用结构体,一个char字符(一个字节)存储多个BOOL值并制定存储位置

 

2.设置类属性BOOL值(setter and getter)

//Person

#import "Person.h"

//mask即掩码,表示二进制数(0b开头)
#define TallMask (1<<0)      //表示1左移0位:0b 0000 0001
#define RichMask (1<<1)      //表示1左移1位:0b 0000 0010
#define HandsomeMask (1<<2)  //表示1左移2位:0b 0000 0100

//拓展:10<<3即在10对应的二进制数后添加3个0

@interface Person()
{
    char _saveBox;
}

@end

@implementation Person

- (instancetype)init
{
    if (self = [super init]) {
        //用一个字节来存储三个变量:从最右往左依次为Tall、Rich、Handsome
        _saveBox = 0b00000101;
    }
    return self;
}

/*思路
 0000 0101(_saveBox)
|0000 0001(掩码)
 ---------
 0000 0001(赋值tall为1)
 
 0000 0101
&1111 1110(掩码取反)
 ---------
 0000 0100(赋值tall为0)
 
 1.如果赋的值为1,则按位或;
 2.如果赋的值为0,则掩码先取反,后按位与;
 */
- (void)setTall:(BOOL)tall
{
    if (tall) {
        _saveBox |= TallMask;
    } else {
        _saveBox &= ~TallMask;
    }
}

- (void)setRich:(BOOL)rich
{
    if (rich) {
        _saveBox |= RichMask;
    } else {
        _saveBox &= ~RichMask;
    }
}

- (void)setHandsome:(BOOL)handsome
{
    if (handsome) {
        _saveBox |= HandsomeMask;
    } else {
        _saveBox &= ~HandsomeMask;
    }
}

/*思路
 0000 0101
&0000 0001
 ---------
 0000 0001(取出tall值)
 
 1.按位与,用掩码取出_saveBox中特定位;
 2.结果>=1,取反为0,再取反为1;同理,为0则双取反后为0;
 */
- (BOOL)isTall
{
    return !!(_saveBox & TallMask);
}

- (BOOL)isRich
{
    return !!(_saveBox & RichMask);
}

- (BOOL)isHandsome
{
    return !!(_saveBox & HandsomeMask);
}

@end

 

//Student

#import "Student.h"

@interface Student()
{
    /*思路
     1.用一个结构体来存放变量;
     2.结构体支持位域:按先后顺序,一个char字符一个字节(0b0000 0000),从最右至左依次为tall、rich、handsome;
     */
    struct {
        char tall : 1;//用一位来存储
        char rich : 1;
        char handsome : 1;
    }_tallRichHandsome;
}

@end

@implementation Student

- (void)setTall:(BOOL)tall
{
    _tallRichHandsome.tall = tall;
}

- (void)setRich:(BOOL)rich
{
    _tallRichHandsome.rich = rich;
}

- (void)setHandsome:(BOOL)handsome
{
    _tallRichHandsome.handsome = handsome;
}

- (BOOL)isTall
{
    return !!_tallRichHandsome.tall;//非0(包括负数)取反为0
}

- (BOOL)isRich
{
    return !!_tallRichHandsome.rich;
}

- (BOOL)isHandsome
{
    return !!_tallRichHandsome.handsome;
}

@end

 

//Worker

#import "Worker.h"

#define TallMask (1<<0)//也可以左移6位,剩余位没用到
#define RichMask (1<<1)
#define HandsomeMask (1<<2)
#define ThinMask (1<<3)

@interface  Worker()
{
    //苹果系统设计思路
    union {
        char bits;//一个字节存储结构体中的所有成员变量
        struct {//摆设用:位域,增加可读性
            char tall : 1;//占一位
            char rich : 1;
            char handsome : 1;
            char thin : 1;
        };
    }_tallRichHandsome;
}

@end

@implementation Worker

- (void)setTall:(BOOL)tall
{
    if (tall) {
        NSLog(@"----%c", _tallRichHandsome.bits);
        _tallRichHandsome.bits |= TallMask;
    } else {
        _tallRichHandsome.bits &= ~TallMask;
    }
}

- (void)setRich:(BOOL)rich
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Client error attempting to chan.. 下一篇iOS---OBJC_ASSOCIATION_ASSIGN可..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目