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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:54:05+00:00 2026-05-30T15:54:05+00:00

Using: SQL Server 2008 R2 My goal is to take a given integer (n)

  • 0

Using: SQL Server 2008 R2

My goal is to take a given integer (n) and select every permutation of integers with n bits in 2 columns, as below.

Given: n=4

Output:

ColA  ColB
0     0
0     0
0     0
0     0
1     0
1     0
1     0
1     1
2     0
2     0
2     1
2     0
3     0
3     0
3     1
3     1
...
14    1
14    1
14    1
14    0
15    1
15    1
15    1
15    1

The output does not, repeat does NOT, need to be ordered, which may or may not make this easier. I do, however, need to be able to set n to any integer… but for the sake of runtime, let’s say it’ll be under 10.

Thanks in advance.

  • 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-30T15:54:06+00:00Added an answer on May 30, 2026 at 3:54 pm

    You might want to try this:

    declare @bits int
    set @bits = 4
    
    ;with AllTheNumbers as (
        select cast (POWER(2, @Bits) as int) - 1 Number
        union all
        select Number - 1
        from AllTheNumbers
        where Number > 0
    ),
    Bits as (
        select @Bits - 1 Bit
        union all
        select  Bit - 1
        from Bits
        where Bit > 0
    )
    select *, case when (Number & cast (POWER(2, Bit) as int)) != 0 then 1 else 0 end
    from AllTheNumbers cross join Bits
    order by Number, Bit desc
    

    AllTheNumbers produces the numbers from 0 to 2^n-1, Bits produces bit numbers from 0 to @Bits – 1 and main part connects them and calculates bit value per position.

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

Sidebar

Related Questions

I am using SQL Server 2008 and I need to select all data from
Using: SQL Server 2008 R2 I'd like to write a query that will select
using sql server 2008 I would like to take a table that has 11
I'm using SQL Server 2008. The task: take an XML file and parse it
Using : SQL Server 2008, Entity Framework I am summing columns in a table
I am using SQL Server 2008-R2 Express Edition. I wrote the query shown below
I'm using SQL Server 2008 R2. SSMS started intermittently hanging every 5 seconds or
Using SQL Server 2008, I need to select this XML into a Table. I
Using SQL Server 2008, is there a way to allow inserts to a table
I'm using SQL Server 2008 with Advanced Services on my Vista Home Premium. I'd

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.