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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:49:23+00:00 2026-05-27T15:49:23+00:00

I came across the following code numdigits = len(cardNumber) oddeven = numdigits & 1

  • 0

I came across the following code

numdigits = len(cardNumber)
oddeven = numdigits & 1

what exactly is going on here? I’m not sure what the "&" is 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-05-27T15:49:24+00:00Added an answer on May 27, 2026 at 3:49 pm

    Answer

    The & symbol is a bitwise AND operator. Used with 1, it basically masks the value to extract the lowest bit, or in other words will tell you if the value is even or odd.

    More Info on Python’s & operator

    For more information, see: http://wiki.python.org/moin/BitwiseOperators

    Why it Works to check Odd vs. Even

    EDIT: Adding this section since this answer is getting some love

    The reason why ANDing a value with 1 tells if the value is odd or even may not be obvious at first.

    The binary representation of a number is essentially the sum of a series of YES or NO for each power of 2 moving leftward starting in the rightmost digit with 1, 2, 4, 8, …

    There is only one way to represent any number in this way. E.g. the number 13 (base 10) can be written in binary as “1101” (or hexadecimal as 0xD, but that’s beside the point). See here:

        1   1   0   1
        x   x   x   x
        8   4   2   1
        =   =   =   =
        8 + 4 + 0 + 1  =  13
    

    Notice that aside from the rightmost binary digit, all other 1 digits will add an even number (i.e. a multiple of 2) to the sum. So the only way to get an odd final sum is to add that odd 1 from the rightmost digit. So if we’re curious if a number is odd or even, we can look at its binary representation and ignore everything except for the rightmost digit.

    To do this, we use the bitwise AND operator. The value 1 in binary is expressed as 1:

        0   0   0   1
        x   x   x   x
        8   4   2   1
        =   =   =   =
        0 + 0 + 0 + 1  =  1
    

    ANDing a value with 1 like this will result in 1 if the value’s rightmost bit is set, and 0 if it is not.

    And because 0 is generally considered “false” in most languages, and non-zero values considered “true”, we can simply say as a shortcut:

    if (value & 1): do_something_with_odd_value()...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've came across the following code, ok, not exactly but close. The point of
I came across the following code and couldn't figure out what was going on.
I came across the following java code. Here interface contains two methods out of
I came across the following code snippet in C++ (I am not yet on
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I came across the following code in a code base I am working on:
I have came across the following code, and being a C beginner, I came
I just came across the following code: for(Iterator<String> iterator = stuff.iterator(); iterator.hasNext();) { ...
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
Today at work we came across the following code (some of you might recognize

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.