首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于位域如何节省内存(C++)

关于位域如何节省内存(C++)

作者头像
Gxjun
发布2018-03-26 16:12:19
7460
发布2018-03-26 16:12:19
举报
文章被收录于专栏:mlml

         位域:  最先使用在c语言中后来C++继承了这一优良的特点。

         举个栗子:     int  -->  4字节   2^32位 ,如果我们只需要其表达一个0~16的数字,

             使用一个int就显得稍稍有些许浪费,所以我们这里就可以使用到位域0~16  --> 2^1 ~ 2^5

              我们就可以这样来声明了:      int   sudo: 5; 就可以了! 

 1 /*
 2    设计一个结构体存储学生的成绩信息,
 3    需要包括学号,年纪和成绩3项内容,学号的范围是0~99 999 999,
 4    年纪分为freshman,sophomore,junior,senior四种,
 5    成绩包括A,B,C,D四个等级。
 6    请使用位域来解决:
 7 */
 8 #include<iostream>
 9 
10 using namespace std;
11 
12   enum Age{ freshman , sophomore , junior ,senior } ;
13 
14   enum grade{ A , B , C , D };
15 
16   class student {
17    private :
18      unsigned int num :27;
19      Age age : 2 ;
20      grade gra:2 ;
21    public  :
22        /* 析构函数 */
23        ~ student(){
24           cout<<"student has been xigou l ! ";
25        }
26        /* 构造函数 */
27        student(unsigned int num , Age age , grade gra );
28       void show();
29   }  ;
30 
31    student::student(unsigned int num , Age age , grade gra ):num(num),age(age),gra(gra){
32    };
33 
34    inline void  student::show(){
35 
36       cout<<"学号为:"<<this->num<<endl;
37       string sag;
38       //采用枚举
39       switch(age){
40           case  freshman :  sag = "freshman" ; break ;
41           case  junior  :   sag = "junior" ; break ;
42           case  senior :    sag = "senior" ; break ;
43           case  sophomore :  sag = "sophomore" ; break ;
44       }
45       cout<<"年纪为:"<<sag<<ends;
46       cout<<"成绩为:"<<char('A'+gra)<<endl;
47    }
48 
49  int main(int args [] ,char argv[]){
50 
51       student stu(12345678,sophomore,C) ;
52       stu.show();
53       cout<<sizeof stu<<endl;
54     return 0;
55  }
 1 /*
 2    编写一个名为CPU的类,描述一个CPU的以下信息:
 3    时钟频率,最大不会超过3000MHZ = 3000*10^6 ;字长,可以
 4    是32位或64位;核数,可以是单核,双核,或四核,是否
 5    支持超线程。各项信息要求使用位域来表示。通过输出sizeof
 6    (CPU)来观察该类所占的字节数。
 7 */
 8  #include<iostream>
 9  #include<string>
10  using namespace std;
11   //字长
12   enum word{ a, b };
13   //核数
14   enum keshu{one ,two ,four };
15   //是否支持超线程
16   enum  es_no{yes ,no} ;
17 
18  class intel_CPU{
19    private :
20     // 2^29 = 536 870 912
21    unsigned int tp : 29 ;
22      word  wd : 1  ;
23      keshu ks : 2 ;
24      es_no en : 1 ;
25    public :
26     //构造函数
27      intel_CPU(int tp=0,word wd=a,keshu ks=two,es_no en=no):
28          tp(tp),wd(wd),ks(ks),en(en){
29           cout<<"this is a construct !"<<endl;
30      };
31      //析构函数
32      ~intel_CPU();
33       void show();
34 
35  };
36     intel_CPU::~intel_CPU(){
37 
38       cout<<"this is a 析构函数!!"<<endl;
39     }
40 
41   void inline intel_CPU::show() {
42 
43       cout<<"时钟频率为:"<<ends;
44       cout<<tp<<endl;
45       cout<<"字长为【32 or 64】: ";
46       if(wd==a)cout<<32<<endl;
47       else cout<<64<<endl;
48       switch (ks){
49         case one :  cout<<"单核"<<endl; break;
50         case two :  cout<<"双核"<<endl; break;
51         case four:  cout<< "四核”"<<endl; break;
52         default :   cout<<"山寨机,没核" ; break;
53      }
54      cout<<"是否支持超线程模式:"<< (en==yes?"支持":"不支持")<<endl;
55   }
56 
57  int main(){
58 
59      intel_CPU  icp(123456789);
60      icp.show();
61      cout<<sizeof icp<<endl;
62 
63  }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-03-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档