Thread: Best approach for tackling search??

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    79

    Best approach for tackling search??

    I am working on an embedded 16 bit platform and have an array that when empty is stuffed with 0xFF. The array depth is 50. During a configuration process this array will take on values 0x00 for array[0], 0x01 for the array[1], 0x02 for array[2] and so on.....There is NO guarantee however that some elements may not be filled in, in a contiguous manner. So for example 0x00 thru 0x03 may fill array[0] thru array[3] but then array[4] and array[5] could be blank and 0xFF and then array[6] = 0x06.

    Given this is embedded with limited resources what would be the best way to ascertain the number of slots that have something other than 0xFF? (not asking how to identify what the values are here). This is a frequent process which will occur and I would like to keep it tight to conserve energy.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    777
    I'd go with the most straightforward solution as it's not a terribly complicated operation:

    Code:
    int number_of_slots_that_have_something_other_than_0xff(const unsigned char array[50])
    {
            int n = 0;
            for (int i = 0; i < 50; ++i) {
                    n += array[i] != 0xFF;
            }
            return n;
    }
    Assuming each iteration takes 5 CPU cycles (it may be more or less, depending on the exact CPU, compiler, and optimization level), that takes only 250 CPU cycles, which shouldn't be too much energy/time consumed.

    On the other hand, instead of counting the slots frequently, maintain a running counter that increases or decreases by one any time a slot changes from 0xFF to something else or vice versa.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array assignment anomaly while tackling Euler Problem 23
    By chrison42 in forum C Programming
    Replies: 2
    Last Post: 03-17-2018, 09:30 AM
  2. Tackling large amounts of code?
    By Cell in forum Tech Board
    Replies: 4
    Last Post: 02-22-2009, 08:15 PM
  3. Best approach
    By DV64h in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-10-2006, 03:24 PM
  4. right approach!
    By tarun in forum C Programming
    Replies: 14
    Last Post: 08-19-2002, 08:00 PM
  5. New approach... using Dev-C++ now...
    By Agent_Worm in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2001, 02:36 PM

Tags for this Thread