Answer to Question #265125 in C++ for acky_1

Question #265125

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


1
Expert's answer
2021-11-12T17:33:01-0500


#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;
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS