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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:43:25+00:00 2026-05-20T06:43:25+00:00

I was reading a question posted here: Sudoku algorithm in C# And one of

  • 0

I was reading a question posted here: Sudoku algorithm in C#

And one of the solutions posted was this piece of code.

public static bool IsValid(int[] values) {
        int flag = 0;
        foreach (int value in values) {
                if (value != 0) {
                        int bit = 1 << value;
                        if ((flag & bit) != 0) return false;
                        flag |= bit;
                }
        }
        return true;
}

The idea is that it will detect duplicates in the array of values; but I’m overwhelmed by how much I don’t know. Can someone explain this to me?

EDIT: Thanks everyone. So many great answers, I don’t know how to select one. It now makes perfect sense.

  • 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-20T06:43:26+00:00Added an answer on May 20, 2026 at 6:43 am

    Really a nice idea.

    Basically, it uses an int flag (initially set to zero) as a “bit array”; for each value, it checks if the corresponding bit in the flag is set, and if it’s not, it sets it.

    If, instead, that bit position is already set, it knows that the corresponding value has already been seen, so the piece of Sudoku is invalid.

    More in detail:

    public static bool IsValid(int[] values)
    {
        // bit field (set to zero => no values processed yet)
        int flag = 0;
        foreach (int value in values)
        {
            // value == 0 => reserved for still not filled cells, not to be processed
            if (value != 0)
            {
                // prepares the bit mask left-shifting 1 of value positions
                int bit = 1 << value; 
                // checks if the bit is already set, and if so the Sudoku is invalid
                if ((flag & bit) != 0)
                    return false;
                // otherwise sets the bit ("value seen")
                flag |= bit;
            }
        }
        // if we didn't exit before, there are no problems with the given values
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this question here: Passing one Dimension of a Two Dimensional Array in
I've posted the same question here a few days ago( Java reading standard output
Reading this question I found this as (note the quotation marks) code to solve
I was reading a question about c# code optimization and one solution was to
I posted this question here before but there were no responses. I may have
I previously posted this question ( Reading Google docs spreadsheet ) and got a
After reading this question , I was reminded of when I was taught Java
After reading this question , i saw the answer by Naveen containing a link
I was reading this question about how to parse URLs out of web pages
I was reading this question (which you do not have to read because 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.