class Employee {
private:
int idNum;
unsigned isFullTime:1;
unsigned deductMedicalInsurance:1;
unsigned deductDentalInsurance:1;
unsigned deductLifeInsurance:1;
unsigned deductUnionDues:1;
unsigned deductSavingsBonds:1;
unsigned deductRetirementPlan:1;
unsigned deductCharitableContribution:1;
};
Set up an objects configuration into a bit field like the above Employee object. Then a unique number can be attained from that configuration.
if(deductMedicalInsurance == 0 && deductDentalInsurance == 1 && deductLifeInsurance == 1 && deductUnionDues == 0 && deductSavingsBonds == 1 && deductRetirementPlan == 0 && deductCharitableContribution == 0)
cout << " Special combination!" << endl;
if(codes == 44)
cout << " Special combination!" << endl;
unsigned Employee::convertBitsToInt()
{
unsigned temp;
temp = deductCharitableContribution * 128
+ deductRetirementPlan * 64
+ deductSavingsBonds * 32
+ deductUnionDues * 16
+ deductLifeInsurance * 8
+ deductDentalInsurance * 4
+ deductMedicalInsurance * 2
+ isFullTime * 1;
return temp;
}
If the bit field is 8 bits, a byte can be derived as an int. Here is the alternative code:
unsigned Employee::getCodes()
{
int* p = &idNum;
++p;
return *p;
}