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

  • Home
  • SEARCH
  • 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 708867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:26:22+00:00 2026-05-14T04:26:22+00:00

I have an array of bytes, in memory. What’s the fastest way to see

  • 0

I have an array of bytes, in memory. What’s the fastest way to see if all the bytes in the array are zero?

  • 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-14T04:26:22+00:00Added an answer on May 14, 2026 at 4:26 am

    Nowadays, short of using SIMD extensions (such as SSE on x86 processors), you might as well iterate over the array and compare each value to 0.

    In the distant past, performing a comparison and conditional branch for each element in the array (in addition to the loop branch itself) would have been deemed expensive and, depending on how often (or early) you could expect a non-zero element to appear in the array, you might have elected to completely do without conditionals inside the loop, using solely bitwise-or to detect any set bits and deferring the actual check until after the loop completes:

    int sum = 0;
    for (i = 0; i < ARRAY_SIZE; ++i) {
      sum |= array[i];
    }
    if (sum != 0) {
      printf("At least one array element is non-zero\n");
    }
    

    However, with today’s pipelined super-scalar processor designs complete with branch prediction, all non-SSE approaches are virtualy indistinguishable within a loop. If anything, comparing each element to zero and breaking out of the loop early (as soon as the first non-zero element is encountered) could be, in the long run, more efficient than the sum |= array[i] approach (which always traverses the entire array) unless, that is, you expect your array to be almost always made up exclusively of zeroes (in which case making the sum |= array[i] approach truly branchless by using GCC’s -funroll-loops could give you the better numbers — see the numbers below for an Athlon processor, results may vary with processor model and manufacturer.)

    #include <stdio.h>
    
    int a[1024*1024];
    
    /* Methods 1 & 2 are equivalent on x86 */  
    
    int main() {
      int i, j, n;
    
    # if defined METHOD3
      int x;
    # endif
    
      for (i = 0; i < 100; ++i) {
    #   if defined METHOD3
        x = 0;
    #   endif
        for (j = 0, n = 0; j < sizeof(a)/sizeof(a[0]); ++j) {
    #     if defined METHOD1
          if (a[j] != 0) { n = 1; }
    #     elif defined METHOD2
          n |= (a[j] != 0);
    #     elif defined METHOD3
          x |= a[j];
    #     endif
        }
    #   if defined METHOD3
        n = (x != 0);
    #   endif
    
        printf("%d\n", n);
      }
    }
    
    $ uname -mp
    i686 athlon
    $ gcc -g -O3 -DMETHOD1 test.c
    $ time ./a.out
    real    0m0.376s
    user    0m0.373s
    sys     0m0.003s
    $ gcc -g -O3 -DMETHOD2 test.c
    $ time ./a.out
    real    0m0.377s
    user    0m0.372s
    sys     0m0.003s
    $ gcc -g -O3 -DMETHOD3 test.c
    $ time ./a.out
    real    0m0.376s
    user    0m0.373s
    sys     0m0.003s
    
    $ gcc -g -O3 -DMETHOD1 -funroll-loops test.c
    $ time ./a.out
    real    0m0.351s
    user    0m0.348s
    sys     0m0.003s
    $ gcc -g -O3 -DMETHOD2 -funroll-loops test.c
    $ time ./a.out
    real    0m0.343s
    user    0m0.340s
    sys     0m0.003s
    $ gcc -g -O3 -DMETHOD3 -funroll-loops test.c
    $ time ./a.out
    real    0m0.209s
    user    0m0.206s
    sys     0m0.003s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have array of bytes: byte[] arr = new byte[] { 0,
I have an array of bytes in my C# class, and need to persist
I have an array of bytes and i want to determine if the contents
I have an array of bytes representing an image in Windows BMP format and
I have dynamic array filled with bytes, which are read from .raw file with
I have a byte array of around 10,000 bytes which is basically a blob
I have a byte array that may or may not have null bytes at
I have a byte that is an array of 30 bytes, but when I
I have a char type array[100] with 100 bytes stored in it. I want
I have c++ code that attempts to dynamically allocate a 2d array of bytes

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.