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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:41:45+00:00 2026-06-18T09:41:45+00:00

Possible Duplicate: Reference – What does this symbol mean in PHP? I am working

  • 0

Possible Duplicate:
Reference – What does this symbol mean in PHP?

I am working on some legacy code where I came across the following function:

function is_odd($number) {
   return $number & 1; // 0 = even, 1 = odd
}

I have never seen a method to check if a number is odd written like that before and am just trying to understand what they actually are doing.

  • 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-18T09:41:46+00:00Added an answer on June 18, 2026 at 9:41 am

    & is a bitwise-and, it basically works so that for every bit that is 1 in both operands, it yields 1 in the resulting value and 0 for all other bits. If you convert any number to its bit representation, you quickly see that it’s the lowest bit that determines whether a number is even or odd, for example:

       5 = 101
      10 = 1010
      13 = 1101
    1030 = 10000000110
    

    The lowest bit (the one on the very right, also called the least significant bit) is 1 for every odd number and 0 for every even number. Doing $n & 1 will always yield 0 for every other bit than the lowest bit (because the number 1 only has one bit, you can imagine that the rest of the bits are left-padded with 0 to match the length of the other operand). So basically the operation boils down to comparing the lowest bit of the operands, and 1 & 1 is 1, all other combinations are 0. So basically when the $n & 1 yields 1, it means the number is odd, otherwise it’s even.

    EDIT.

    Here’s a few examples to demonstrate how the bitwise-and works for the example values I gave earlier, the number in the parenthesis is the original decimal number:

      101 (5)
    & 001 (1)
      ---
      001 (1) = true
    
    
      1010 (10)
    & 0001 (1)
      ----
      0000 (0) = false
    
    
      1101 (13)
    & 0001 (1)
      ----
      0001 (1) = true
    
    
      10000000110 (1030)
    & 00000000001 (1)
      -----------
      00000000000 (0) = false
    

    From this you can easily see that the result is only true when both operands’ right-most bits are 1.

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

Sidebar

Related Questions

Possible Duplicate: Reference - What does this symbol mean in PHP? Consider this code
Possible Duplicate: Reference - What does this symbol mean in PHP? I have some
Possible Duplicate: Reference - What does this symbol mean in PHP? I have a
Possible Duplicate: Reference - What does this symbol mean in PHP? What's the difference
Possible Duplicate: Reference - What does this symbol mean in PHP? I am trying
Possible Duplicate: Reference - What does this symbol mean in PHP? I don't know
Possible Duplicate: Reference - What does this symbol mean in PHP? I am try
Possible Duplicate: Reference - What does this symbol mean in PHP? When I use
Possible Duplicate: Reference - What does this symbol mean in PHP? In PHP what
Possible Duplicate: Reference - What does this symbol mean in PHP? Is there any

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.