about how bit fields save memory (c + +)

Source: Internet
Author: User

Bit field: First used in the C language later C + + inherits this excellent feature.

For a chestnut: INT--and 4 bytes 2^32 bit, if we only need it to express a 0~16 number,

Using an int is a bit wasteful, so we can use the field 0~16---2^1 ~ 2^5

We can declare it this way: int sudo:5; You can do it!

1 /*2 design a structure to store students ' performance information,3 need to include school number, age and score 3 items, the range of the study number is 0~99 999 999,4 age is divided into four species of Freshman,sophomore,junior,senior,5 scores include A,b,c,d four levels. 6 use bit fields to resolve:7 */8#include <iostream>9 Ten using namespacestd; One  A   enumage{freshman, sophomore, Junior, senior}; -  -   enumgrade{A, B, C, D}; the  -   classStudent { -    Private : -UnsignedintNum: -; +Age:2 ; -Grade GRA:2 ; +     Public  : A        /* Destructors*/ at~student () { -cout<<"student has been Xigou L!"; -        } -        /*constructor Function*/ -Student (unsignedintnum, age, grade GRA); -       voidShow (); in   }  ; -  toStudent::student (unsignedintnum, age, Grade GRA): num (num), age, GRA (GRA) { +    }; -  theInlinevoidstudent::show () { *  $cout<<"The study number is:"<< This->num<<Endl;Panax Notoginseng       stringsag; -       //Take enumeration the       Switch(age) { +            CaseFreshman:sag ="Freshman"; Break ; A            CaseJunior:sag ="Junior"; Break ; the            CaseSenior:sag ="Senior"; Break ; +            CaseSophomore:sag ="Sophomore"; Break ; -       } $cout<<"Age is:"<<sag<<ends; $cout<<"results are:"<<Char('A'+gra) <<Endl; -    } -  the  intMainintargs [],Charargv[]) { - WuyiStudent Stu (12345678, sophomore,c); the stu.show (); -cout<<sizeofstu<<Endl; Wu     return 0; -}
View Code

1 /*2 write a class called CPU that describes the following information for a CPU:3 clock frequency, maximum no more than 3000MHZ = 3000*10^6; Word length, can4 is 32-bit or 64-bit; the number of cores can be single-core, dual-core, or quad-core, whether5 Hyper-Threading is supported. The information requirements are expressed using bit fields. by outputting sizeof6 (CPU) to observe the number of bytes that the class occupies. 7 */8#include <iostream>9#include <string>Ten  using namespacestd; One   //Word length A   enumword{A, b}; -   //Number of cores -   enumKeshu{one, both, four}; the   //whether Hyper-Threading is supported -   enumEs_no{yes, no}; -  -  classintel_cpu{ +    Private : -     //2^29 = 536 870 912 +UnsignedintTp: in ; AWord WD:1  ; atKeshu KS:2 ; -Es_no en:1 ; -     Public : -     //constructor Function -INTEL_CPU (inttp=0, Word Wd=a,keshu ks=two,es_no en=No): - TP (TP), WD (WD), KS (KS), en (en) { incout<<"This is a construct!"<<Endl; -      }; to      // Destructors +~intel_cpu (); -       voidShow (); the  *  }; $intel_cpu::~intel_cpu () {Panax Notoginseng  -cout<<"This is a destructor!! "<<Endl; the     } +  A   voidinline Intel_cpu::show () { the  +cout<<"the clock frequency is:"<<ends; -cout<<tp<<Endl; $cout<<"The word length is "64 or":"; $       if(wd==a) cout<< +<<Endl; -       Elsecout<< -<<Endl; -       Switch(KS) { the          Caseone:cout<<"Single Core"<<endl; Break; -          Casetwo:cout<<"Dual-Core"<<endl; Break;Wuyi          Casefour:cout<<"Four Cores ""<<endl; Break; the         default: cout<<"Shanzhai, no nuclear."; Break; -      } Wucout<<"whether Hyper-threading mode is supported:"<< (En==yes?"Support":"not supported") <<Endl; -   } About  $  intMain () { -  -INTEL_CPU ICP (123456789); - icp.show (); Acout<<sizeoficp<<Endl; +  the}

about how bit fields save memory (c + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.