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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:02:51+00:00 2026-05-14T20:02:51+00:00

When you have a circular buffer represented as an array, and you need the

  • 0

When you have a circular buffer represented as an array, and you need the index to wraparound (i.e., when you reach the highest possible index and increment it), is it “better” to:

return (++i == buffer.length) ? 0: i;

Or

return ++i % buffer.length;

Has using the modulo operator any drawbacks? Is it less readable than the first solution?

EDIT:

Of course it should be ++i instead of i++, changed that.

EDIT 2:

One interesting note: I found the first line of code in ArrayBlockingQueue’s implementation by Doug Lea.

  • 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-14T20:02:51+00:00Added an answer on May 14, 2026 at 8:02 pm

    Update: OP has admitted in a comment that it should have been pre-increment instead. Most of the other answers missed this. There lies proof that the increment in this scenario leads to horrible readability: there’s a bug, and most people couldn’t see it.

    The most readable version is the following:

    return (i == buffer.length-1) ? 0 : i+1;
    

    Using ++ adds unnecessary side effect to the check (not to mention that I strongly feel that you should’ve used pre-increment instead)

    What’s the problem with the original code? Let’s have a look, shall we?

    return (i++ == N) ? 0 : i; // OP's original, slightly rewritten
    

    So we know that:

    • i is post-incremented, so when i == N-1 before the return statement, this will return N instead of wrapping to 0 immediately
      • Is this intended? Most of the time, the intent is to use N as an exclusive upper bound
    • The variable name i suggests a local variable by naming convention, but is it really?
      • Need to double check if it’s a field, due to side-effect

    In comparison:

    return (i == N-1) ? 0 : i+1; // proposed alternative
    

    Here we know that:

    • i is not modified, doesn’t matter if it’s local variable or field
    • When i == N-1, the returned value is 0, which is more typical scenario

    The % approach

    Alternatively, you can also use the % version as follows:

    return (i+1) % N;
    

    What’s the problem with %? Well, the problem is that even though most people think it’s the modulo operator, it’s NOT! It’s the remainder operator (JLS 15.17.3). A lot of people often get this confused. Here’s a classic example:

    boolean isOdd(int n) {
       return (n % 2) == 1; // does this work???
    }
    

    That code is broken!!! It returns false for all negative values! The problem is that -1 % 2 == -1, although mathematically -1 = 1 (mod 2).

    % can be tricky, and that’s why I recommend the ternary operator version instead. The most important part, though, is to remove the side-effect of the increment.

    See also

    • Wikipedia: modulo operation
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fixed size circular buffer (implemented as an array): upon initialization, the
Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
I'm using pthread on Linux. I have a circular buffer to pass data from
I'm trying to implement a circular buffer in C, and have come across this
I have a circular, statically allocated buffer in C, which I'm using as a
I have written a template class for a circular buffer: template <class T> class
I have seen the following implementation of circular buffer: http://en.wikipedia.org/wiki/Circular_buffer /**< Buffer Size */
Every Java circular byte buffer implementation I have seen referenced on SO and elsewhere
I'm trying to write a finalizer for Python classes that have circular references. I
I am trying to have a circular overlay come to the top when 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.