In a Military database system, IDs of the army personnel are stored in a 32-bit value
which is coded as follows:
a. 7-bits Belt number
b. 10-bits Batch number
c. 5-bits Log number
d. 10-bits Unit number
Your Task is to write a C++ Program which inputs a four-byte integer ID, and a string Name of the
army man. Your Program will separate the Belt number, Batch number, Log number and Unit
number and prints the information in the following manner.
Enter Name of Army Man Khan
Enter ID of Army Man: 858993459
Belt number of Khan is : 51
Batch number of Khan is: 614
Log number of Khan is: 25
Unit number of Khan is: 204
#include <bits/stdc++.h>
using namespace std;
int bits(int n, int x, int i)
{
return (((1 << x) - 1) & (n >> (i - 1)));
}
int main()
{
cout<<"Enter Name of Army Man ";
string name;
cin>>name;
cout<<"Enter ID of Army Man: ";
int id;
cin>>id;
cout<<"Belt number of "<<name<<" is: "<<bits(id,7,4)<<endl;
cout<<"Batch number of "<<name<<" is: "<<bits(id,10,4)<<endl;
cout<<"Log number of "<<name<<" is: "<<bits(id,5,4)<<endl;
cout<<"Unit number of "<<name<<" is: "<<bits(id,10,4)<<endl;
return 0;
}
Comments
Leave a comment