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 8583619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:34:49+00:00 2026-06-11T21:34:49+00:00

I have a byte array unsigned char* array=new unsigned char[4000000]; … And I would

  • 0

I have a byte array

unsigned char* array=new unsigned char[4000000];
 ...

And I would like to get indices of all non-zero elements of the array.

Of course, I can do following

for(int i=0;i<size;i++)
{
    if(array[i]!=0) somevector.push_back(i);
}

Is there any faster algorithm than this?

Update 1 I can see majority answer is no. I hoped that there is some magical bit operations I am not aware of. Some guys suggested sorting but no it’s not feasible in this case. But thanks a lot for all your answers.

Update 2 After 4 years and 4 months since this question posted, @wim suggested this answer that looks promising.

  • 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-06-11T21:34:50+00:00Added an answer on June 11, 2026 at 9:34 pm

    With a byte array that is mostly zero, being a sparse array, you can take advantage of a 32 bit CPU by doing comparisons 4 bytes at a time. The actual comparisons are done 4 bytes at a time however if any of the bytes are non-zero then you have to determine which of the bytes in the unsigned long are non-zero so that will take more effort. If the array is really sparse then the time saved with the comparisons may compensate for the additional work determining which of the bytes are non-zero.

    The easiest would be to make the unsigned char array sized to some multiple of 4 bytes so that you do not need to worry about doing the last few bytes after the loop completes.

    I would suggest doing a timing study on this as it is purely conjectural and there would be a point where an array becomes un-sparse enough that this would take more time than a simple loop.

    One question that I would have is what are you doing with the vector of offsets of non-zero elements of the array and whether you can do away with the vector. Another question is if you need the vector whether you can build the vector as you place elements into the array.

    unsigned char* array=new unsigned char[4000000];
    ......
    unsigned long *pUlaw = (unsigned long *)array;
    
    for ( ; pUlaw < array + 4000000; pUlaw++) {
        if (*pUlaw) {
            // at least one byte is non-zero
            unsigned char *pUlawByte = (unsigned char *)pUlaw;
            if (*pUlawByte)
                somevector.push_back(pUlawByte - array);
            if (*(pUlawByte+1))
                somevector.push_back(pUlawByte - array + 1);
            if (*(pUlawByte+2))
                somevector.push_back(pUlawByte - array + 2);
            if (*(pUlawByte+3))
                somevector.push_back(pUlawByte - array + 3);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte (unsigned char) array. How to draw it using QPainter?
I wanted to convert array< Byte>^ to unsigned char*. I have tried to explain
I have a unsigned char array[248]; filled with bytes. Like 2F AF FF 00
I have a byte array of size 4 byte[] source = new byte[4]; Now
I have an unsigned char array whose size is 6. The content of the
I have a unsigned int[16] array that when printed out looks like this: 4418703544ED3F688AC208F53343AA59
I have a helper function that takes an unsigned char array of a fixed
I have a function which looks something like this void doSomething(unsigned char buff[50]); What
I have a byte array which needs to be marshalled into the following struct:
I have a byte array and I need to convert this to a image.

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.