设为首页 加入收藏

TOP

位域-isa指针(三)
2019-10-09 20:06:23 】 浏览:113
Tags:位域 -isa 指针
{
if (rich) { _tallRichHandsome.bits |= RichMask; } else { _tallRichHandsome.bits &= ~RichMask; } } - (void)setHandsome:(BOOL)handsome { if (handsome) { _tallRichHandsome.bits |= HandsomeMask; } else { _tallRichHandsome.bits &= ~HandsomeMask; } } - (void)setThin:(BOOL)thin { if (thin) { _tallRichHandsome.bits |= ThinMask; } else { _tallRichHandsome.bits &= ~ThinMask; } } - (BOOL)isTall { return !!(_tallRichHandsome.bits & TallMask); } - (BOOL)isRich { return !!(_tallRichHandsome.bits & RichMask); } - (BOOL)isHandsome { return !!(_tallRichHandsome.bits & HandsomeMask); } - (BOOL)isThin { return !!(_tallRichHandsome.bits & ThinMask); } @end

 

//main

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"
#import "Worker.h"
#import "Engineer.h"

struct bs {
    unsigned a : 9;//如果超过位域范围(511),则只取范围内的值,其他位(高位)丢弃
    unsigned b : 4;
    unsigned c : 3;
}bit, *pbit;

void test1()
{
    bit.a = 512;//超过位域范围报警告
    bit.b = 10;
    bit.c = 7;
    NSLog(@"%d,%d,%d\n", bit.a, bit.b, bit.c);
    
    pbit=&bit;
    pbit-> a=0;
    pbit-> b&=3;
    pbit-> c|=1;
    printf("%d,%d,%d\n ",pbit-> a,pbit-> b,pbit-> c);
}

void test2()
{
    Person *per = [[Person alloc] init];
    per.tall = NO;
    per.rich = NO;
    per.handsome = YES;
    NSLog(@"%d %d %d", per.isTall, per.isRich, per.isHandsome);
}

void test3()
{
    Student *stu = [[Student alloc] init];
    stu.tall = YES;
    stu.rich = NO;
    stu.handsome = YES;
    NSLog(@"%d %d %d", stu.isTall, stu.isRich, stu.isHandsome);
}

void test4()
{
    Worker *worker = [[Worker alloc] init];
//    worker.tall = YES;
    worker.rich = NO;
    worker.handsome = NO;
    worker.thin = YES;
    NSLog(@"%d %d %d", worker.isThin, worker.isRich, worker.isHandsome);
}

void test5()
{
    Engineer *engineer = [[Engineer alloc] init];
//    engineer.age = 12;
//    engineer.level = 6;
//    engineer.workers = 5;
    
    //0b 1111 1111 1111 1111(十进制:65535)
    //0b 0010 1100 1110 1101(十进制:11501)
    engineer->_personalInfo.bits =11501;
    NSLog(@"%d %d %d", engineer.getAge, engineer.getLevel, engineer.getWorkers);
    //2019-10-08 16:42:09.612140+0800 SetAndGetsForMask[1488:127227] 7 16 8160
    //
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        test1();
//        test2();
//        test3();
//        test4();
//        test5();
    }
    return 0;
}

 

//打印

2019-10-09 10:42:04.998750+0800 SetAndGetsForMask[2513:316066] 0 0 1
2019-10-09 10:42:04.999093+0800 SetAndGetsForMask[2513:316066] 1 0 1
2019-10-09 10:42:04.999122+0800 SetAndGetsForMask[2513:316066] 1 0 0
Program ended with exit code: 0

//分析(以Worker为例)

1)共用体中所有成员共同占用一块内存区,其大小等于最大那个成员所占字节数;

2)Worker中的结构体并为定义变量,编译器不会计算其内存,仅是增加可读性;

3)Worker中只有一个char型变量bits(占一个字节),故该共用体变量_tallRichHandsome也占一个字节;

4)结构体的位域限制变量的取值范围(一位:即0或1),mask掩码规定该变量存储的位置(在哪一位上);

 

3.设置类属性非BOOL类型(setter and getter)——限定变量值范围且指定存储位置

//Engineer

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

//位域位置(变量值存储位置)
#define AgeMask 0b00000111//最低三位存储
#define LevelMask (1<<4)//低位往高位数,第5位存储
#define WorkersMask 0b0001111111100000
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Client error attempting to chan.. 下一篇iOS---OBJC_ASSOCIATION_ASSIGN可..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目