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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:31:38+00:00 2026-06-07T15:31:38+00:00

Looking at some code written by another developer, I came across this: for($i=1; $i<=30;

  • 0

Looking at some code written by another developer, I came across this:

for($i=1; $i<=30; $i++)
{
  if($i&1)
    $color = '#fff';
  else
    $color = '#bbb';
}

This $color variable is used for row background colour later in the code. The alternating colours work fine.

If I was writing this, I would have used the modulus operator (%) rather than the bitwise (&) operator.

Why does the bitwise operator work in this case? Is there any advantage of using this method rather than the modulus operator?

  • 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-07T15:31:39+00:00Added an answer on June 7, 2026 at 3:31 pm

    The & operator does a bitwise comparison on the number. So if you do

    $i & 1

    it will then tell you if the ‘1’ flag is set, such as in binary:

    001010111010

    The last number is the ‘1’ flag (remember, binary goes 1, 2, 4, 8 etc. in reverse order), which in this case is set to 0.

    Since 1 is the only odd flag in binary, it will tell you if the number is odd or even.

    if $i is 3 for example, then in binary it will be 011 – the last number is a 1 (the 1 flag) and thus $i & 1 will be true.

    if $i is 4 for example, then in binary it will be 100 – the last number is a 0 (the 1 flag) and thus $i & 1 will be false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking through some code for work, and came across this line: eval(\$element
I came across some code written in VS7.1 and now I'm trying to get
I'm looking through some code for learning purposes. I'm working through this portion of
So I'm looking through some code, and I see this: class whatever { public:
I’m working on some expression tree code written by a colleague and am looking
I'm looking at some code written by a former employee where I work and
I was looking at some code and saw something like this: int d =
When looking through some code that was handled by another employee, I see a
I was looking through some C++ code written by a (now departed) coworker quite
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I

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.