bitset solution

M

ma740988

The code below works and achieves my objective. Something tells me
there's a solution that utilizes bitset, hence I'm soliciting a
implementation approach (i'm trying to train myself to think C++
Standard Library but even with book - Josuttis - C++ Standard .. - in
my hand, I'm going around in circles trying to revise the code).
Thanks.

# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

enum recipients { PDP1 = 1,
PDP2,
PDP3,
PDP4,
PDP5,
PDP6 };

typedef unsigned int uint_type;
uint_type const num_proc_per_pdp(4);
uint_type const num_pdp(6);
uint_type const pdp_total = num_pdp * num_proc_per_pdp;

typedef std::vector<uint_type> PDP_VEC;
void lookup_recipients(
PDP_VEC& recipient_vec,
uint_type const desired_mask
)
{
/*
| what to do: Given a desired_mask I have to look for a 'recipient' in
a
| 4 bit range - up to 6 recipients (int 4 bytes on
system).
| So now:
| bits[0..3 ] - the range 0 to 3 - recipient = 1 (i.e store 1 in
vec);
| bits[4..7 ] - the range 4 to 7 - recipient = 2 (i.e store 2 in
vec);
| bits[8.. 11] - the range 8 to 11 - recipient = 3 (i.e store 3 in
vec);
| bits[12..15] - the range 12 to 15 - recipient = 4 (i.e store 4 in
vec);
| bits[16..19] - the range 16 to 19 - recipient = 5 (i.e store 5 in
vec);
| bits[20..23] - the range 20 to 23 - recipient = 6 (i.e store 6 in
vec);
| 24 to 31 is a don't care.
*/

recipient_vec.clear(); // clear vector of recipients
recipient_vec.reserve(num_pdp); // clear vector of recipients

if (desired_mask >= 0x1000000) return;
cout << " desired mask 0x" << std::hex << desired_mask << endl;

for (uint_type recip = PDP1; recip <= PDP6; ++recip)
{
bool success(false);
uint_type lsp_mask =
0x00000001 << ( num_proc_per_pdp * ( recip - 1 ));
//std::cout << " lsp_mask " << lsp_mask << std::endl;

uint_type start_bit(num_proc_per_pdp * (recip - 1));
uint_type const stop_bit(num_proc_per_pdp * recip);

for (; start_bit < stop_bit; ++start_bit)
{
uint_type const value = desired_mask & lsp_mask;
if (value)
{
//std::cout << " start_bit "
// << start_bit
// << std::endl;
success = true;
}
lsp_mask <<= 1;
}
if (success)
{
recipient_vec.push_back(recip);
}
}
}
int main()
{
PDP_VEC pd;
lookup_recipients(pd, 0xFFFFFF);
std::copy (pd.begin(), pd.end(),
std::eek:stream_iterator<int>(std::cout));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top