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

  • Home
  • SEARCH
  • 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 73673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:14:18+00:00 2026-05-10T20:14:18+00:00

I am trying to find an algorithm to count from 0 to 2 n

  • 0

I am trying to find an algorithm to count from 0 to 2n-1 but their bit pattern reversed. I care about only n LSB of a word. As you may have guessed I failed.

For n=3:

000 -> 0 100 -> 4 010 -> 2 110 -> 6 001 -> 1 101 -> 5 011 -> 3 111 -> 7 

You get the idea.

Answers in pseudo-code is great. Code fragments in any language are welcome, answers without bit operations are preferred.

Please don’t just post a fragment without even a short explanation or a pointer to a source.

Edit: I forgot to add, I already have a naive implementation which just bit-reverses a count variable. In a sense, this method is not really counting.

  • 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. 2026-05-10T20:14:19+00:00Added an answer on May 10, 2026 at 8:14 pm

    This is, I think easiest with bit operations, even though you said this wasn’t preferred

    Assuming 32 bit ints, here’s a nifty chunk of code that can reverse all of the bits without doing it in 32 steps:

     unsigned int i;  i = (i & 0x55555555) <<  1 | (i & 0xaaaaaaaa) >>  1;  i = (i & 0x33333333) <<  2 | (i & 0xcccccccc) >>  2;  i = (i & 0x0f0f0f0f) <<  4 | (i & 0xf0f0f0f0) >>  4;  i = (i & 0x00ff00ff) <<  8 | (i & 0xff00ff00) >>  8;  i = (i & 0x0000ffff) << 16 | (i & 0xffff0000) >> 16;  i >>= (32 - n); 

    Essentially this does an interleaved shuffle of all of the bits. Each time around half of the bits in the value are swapped with the other half.

    The last line is necessary to realign the bits so that bin ‘n’ is the most significant bit.

    Shorter versions of this are possible if ‘n’ is <= 16, or <= 8

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Yes. This tutorial should be of help to get started. May 11, 2026 at 2:36 pm
  • added an answer Set ShowIcon Property of the form to False to see… May 11, 2026 at 2:36 pm
  • added an answer I assume you're using a VCS? If so my intuition… May 11, 2026 at 2:36 pm

Related Questions

I am working on a group senior project for my university and I have
I have a set of students (referred to as items in the title for
I am trying to determine the best time efficient algorithm to accomplish the task
I am trying to figure out what the day of the week of day

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.