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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:47:14+00:00 2026-05-13T15:47:14+00:00

Hey, if you can get a more descriptive tittle please edit it. I’m writing

  • 0

Hey, if you can get a more descriptive tittle please edit it.

I’m writing a little algorithm that involves checking values in a matrix.
Let’s say:

char matrix[100][100];
char *ptr = &matrix[0][0];

imagine i populate the matrix with a couple of values (5 or 6) of 1, like:

matrix[20][35]=1;
matrix[67][34]=1;

How can I know if the binary value of an interval of the matrix is zero, for example (in pseudo code)

if((the value from ptr+100 to ptr+200)==0){ ... // do something

I’m trying to pick up on c/c++ again. There should be a way of picking those one hundred bytes (which are all next to each other) and check if their value is all zeros without having to check on by one.(considering char is one byte)

  • 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-13T15:47:14+00:00Added an answer on May 13, 2026 at 3:47 pm

    You can use std::find_if.

    bool not_0(char c)
    {
        return c != 0;
    }
    
    char *next = std::find_if(ptr + 100, ptr + 200, not_0);
    if (next == ptr + 200)
        // all 0's
    

    You can also use binders to remove the free function (although I think binders are hard to read):

    char *next = std::find_if(ptr + 100, ptr + 200,
                               std::bind2nd(std::not_equal_to<char>(), 0));
    

    Dang, I just notice request not to do this byte by byte. find_if will still do byte by byte although it’s hidden. You will have to do this 1 by 1 although using a larger type will help. Here’s my final version.

    template <class T>
    bool all_0(const char *begin, const char *end, ssize_t cutoff = 10)
    {
        if (end - begin < cutoff)
        {
            const char *next = std::find_if(begin, end,
               std::bind2nd(std::not_equal_to<char>(), 0));
            return (next == end);
        }
        else
        {
            while ((begin < end) && ((reinterpret_cast<uintptr_t>(begin) % sizeof(T)) != 0))
            {
                if (*begin != '\0')
                    return false;
    
                ++begin;
            }
    
            while ((end > begin) && ((reinterpret_cast<uintptr_t>(end) % sizeof(T)) != 0))
            {
                --end;
    
               if (*end != '\0')
                   return false;
            }
    
            const T *nbegin = reinterpret_cast<const T *>(begin);
            const T *nend = reinterpret_cast<const T *>(end);
    
            const T *next = std::find_if(nbegin, nend,
               std::bind2nd(std::not_equal_to<T>(), 0));
            return (next == nend);
        }
    }
    

    What this does is first checks to see if the data is long enough to make it worth the more complex algorithm. I’m not 100% sure this is necessary but you can tune what is the minimum necessary.

    Assuming the data is long enough it first aligns the begin and end pointers to match the alignment of the type used to do the comparisons. It then uses the new type to check the bulk of the data.

    I would recommend using:

    all_0<int>(); // 32 bit platforms
    all_0<long>(); // 64 bit LP64 platforms (most (all?) Unix platforms)
    all_0<long long>() // 64 bit LLP64 platforms (Windows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. Trying to get a little more efficient with lists in Python but
Hey. How can I get a total number of rows in a file (do
Hey all, I've been trying to get a series of objects to appear that
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey can any one tell me should i need open a port for outgoing
Hey guys how can I make an app keep playing an mp3 after pressed
Hey all how can i set this up for a loop? data.row9_1 I cant
Hey everyone I got a javascript problem I can't seem to find a specific
Hey, I can't seem to access the returned value from a method I called
Hey guys I'm trying to make a GUI which can navigate through JTextAreas when

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.