Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:49:39+00:00 2026-05-26T03:49:39+00:00

This is a different question from the one I just asked and it is

  • 0

This is a different question from the one I just asked and it is more challenging.

I have an unsigned char array, say
unsigned char A[16].
I need to generate a mask vector which i will apply to my array A[16].

It should contain n number of ‘1’s, where 0 < n < 16*8 (The mask vector can be an array B[16] as long as there are n number of ‘1’s in the array)

I also need these n number of ‘1’s distributed randomly in the vector.

How can I do this in c/c++?

Thank you!

Edit:
My thought is as follows:
I will generate n random numbers (checking needs to be done to make sure all n numbers are not the same) and store them in array tmp[n]. Then mask is generated based on shifting.

srand(time(0));
for(i = 0; i < n; i++){
  for(j = 0; j < i; j++) 
    while(tmp[i] == tmp[j])  // to make sure all n random numbers are different
      tmp[i] = rand()%128;

unsigned char mask[16] 
for(i = 0; i < n; i++) 
  mask[16] |= (1 << tmp[i]);  //generate mask
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:49:39+00:00Added an answer on May 26, 2026 at 3:49 am

    Generate random (i,j) pair of numbers, where i < 16 and j < 8. If the bit at position B[i]&(1<<j) is not set, set it and increment “count”. Loop until “count” reaches “n”.

    A bit of code (untested):

    void generate_n_bit_mask ( unsigned char B[], int n )
    {
        // avoid infinite loop later on.
        for ( int i=0; (i < 16); ++i ) {
            B[i] = 0;
        }
        // invariant: k is number of currently masked bits.
        for ( int k = 0; (k < n); )
        {
            // select bit at random.
            int i = rand() % 16;
            int j = rand() %  8;
            unsigned char mask = 1 << j;
            // set it if not selected previously.
            if ( (B[i]&mask) == 0 ) {
                B[i] |= mask, ++k;
            }
        }
    }
    

    Exercise, for the challenge: remove magic constant 16 from the code.

    Edit: The modification suggested in your comments contains a nasty bug. Here is a test program to play with the way bits are distributed in your output mask.

    #include <iostream>
    #include <iomanip>
    #include <ctime>
    
    void generate_n_bit_mask ( unsigned char B[], int n )
    {
        // avoid infinite loop later on.
        for ( int i=0; (i < 16); ++i ) {
            B[i] = 0;
        }
        // invariant: k is number of currently masked bits.
        for ( int k = 0; (k < n); )
        {
            // select bit at random.
            int i = std::rand() % 16;
            int j = std::rand() %  8;
            unsigned char mask = 1 << j;
            // set it if not selected previously.
            if ( (B[i]&mask) == 0 ) {
                B[i] |= mask, ++k;
            }
        }
        int j = 0;
    }
    
    // count number of set bits in a byte.
    int bit_count ( unsigned char x )
    {
        int n = 0;
        for ( int i = 0; (i < 8); ++i ) {
            n += ((x >> i) & 1);
        }
        return (n);
    }
    
    // count number of set bits in 16 bytes.
    int total_bit_count ( unsigned char B[] )
    {
        int n = 0;
        for ( int i = 0; (i < 16); ++i ) {
            n += bit_count(B[i]);
        }
        return (n);
    }
    
    int main ( int, char ** )
    {
        std::srand(std::time(0));
        unsigned char B[16];
        // for all possible values of "n"
        for ( int i = 0; (i <= 16*8); ++i )
        {
            // generate a 16 byte mask with "n" set bits.
            generate_n_bit_mask(B, i);
            // verify that "n" bits are set.
            int n = total_bit_count(B);
            if ( n != i ) {
                std::cout << i << ": " << n << std::endl;
            }
        }
    }
    

    When this program is run, it will try every value of n from 0 to 16*8 and generate a random mask with n bits, then verify that exactly n bits are set. If any error occurs (for some value of n, some k!=n bits are set), a message is output.

    If I change the condition to if ( (B[i]^mask) != 0 ), I get consistent errors in the output. Every run produces at least 1 error message. The original condition if ( (B[i]&mask) == 0 ) consistently produces 0 error messages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked before ( link ) but I have slightly different
I have seen this question asked in a couple of different ways on SO
(In case this seems familiar: I asked a different question similar to this one
[Please note that this is a different question from the already answered How to
This is similar to (but different from) this question . Here is some simple
(Similar in spirit to but different in practice from this question .) Is there
I'm re-asking this question but with a different framework this time. I have two
I asked a question about different testing frameworks yesterday. This question can be found
I have just asked these two questions, one on flash seo url best practices
There may be more than one way to ask this question, so here's a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.