dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: All
Apr 17, 1998

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Use Bit Fields to Store Significant Amounts of Data Optimally

Both C and C++ enable you to store and access data directly in the tiniest possible unit: a bit. This technique is required when dealing with a huge amount of data, for example, on databases containing dozens of millions of records; when memory is scarce (i.e., in embedded systems, hand watch phone books) or when transmitted via a slow communication link. In the following example, only one byte is used to store a person's data, instead of three which would have been needed otherwise. When multiplied by a factor of 10 million, for example a national registry's records, it does make a difference:
 
struct PersonalData {
unsigned int marital_stat: 3;  //3 bits
unsigned int gender: 1;		//1 bit
unsigned int children: 4;	//4 bits
};
enum Stat {single, married, divorced, widowed, separated};
enum Gender {male, female};

void f () {
	PersonalData pd [1000]; //1000 bytes

	//fill with data
	pd[0].marital_stat = married;
	pd[0].gender = female;
	pd[0].children = 2; //ints can be used but its the user's
//responsibilty to avoid overflow. Enum's are
//safe, though //...etc. TrasmitToClient(&pd[0], 1000); //transmit all of them }
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date