### a C++ Program which inputs a four-byte integer

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
can anyone solve or give a hint how to do this
So what have you tried?

Or are you just dumping your homework again.

4 posts and you haven't shown any code yet.
no that is not the case sir i have done all my homework except these 3 questions i dont know where to even begin with these,any help will be appericated
bitfield will do all the work for you. it may be good practice, or the intent, to do it 'by hand'.
to do it by hand, take your unsigned 32 bit value and shift (>>) it down until you have just the belt number. extract the belt number. Now zero out the top 7 bits, which you can do with &. That is, number & 000000011111111111111...etc (binary) but use a hex or decimal constant once you work out what the value is (calc in windows understand binary..).
beware of 'endian' issues; make sure you are poking at the correct bits.
anyway, once you zero out the top 7 bits, you can shift it again to get the next group, repeat the same idea (different constants each go though) until you pulled off all the values.

basically, you really need to understand binary numbers, the shift, and, or, not, xor bit-wise logic operators and if you understand those, this will be very easy.
there are more efficient/smarter ways to do this (still not counting bitfield/bitset); the above idea is the most basic brute force way.
bitfield/bitset is the right way to do it, but again, hands on will teach you something.
Last edited on
> bitfield will do all the work for you.

It may, on certain implementations; and lead to general insanity on others.

 The following properties of bit fields are implementation-defined: ... Everything about the actual allocation details of bit fields within the class object . For example, on some platforms, bit fields don't straddle bytes, on others they do . Also, on some platforms, bit fields are packed left-to-right, on others right-to-left https://en.cppreference.com/w/cpp/language/bit_field#Notes
 i dont know where to even begin with these

 write a C++ Program which inputs an integer ID, and a string

You surely have something like that in the other homework?
You're expected to use bits mask to extract the different parts of the full 32bit value.
To extract the data from the id, consider simply:

 ``12345678910111213141516171819`` ``````#include int main() { uint32_t id {}; //std::cout << "Enter id: "; //std::cin >> id; id = 858993459u; const auto belt{ id & 127u }; const auto batch { (id >>= 7) & 1023u}; const auto log{ (id >>= 10) & 31u}; const auto unit{ (id >>= 5) & 1023u }; std::cout << "belt: " << belt << '\n'; std::cout << "batch: " << batch << '\n'; std::cout << "log: " << log << '\n'; std::cout << "unit: " << unit << '\n'; }``````

 ``` belt: 51 batch: 614 log: 25 unit: 204 ```

Last edited on
@seeplus: You're really not supposed to do homework for folk.
Topic archived. No new replies allowed.