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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:17:44+00:00 2026-06-01T15:17:44+00:00

I have a bit field consisting of 64 bits: long bitfield = 0; I

  • 0

I have a bit field consisting of 64 bits:

long bitfield = 0;

I can set the bit for a given index as follows:

void Set(long index)
{
   bitfield |= 1L << (int)(index % 64);
}

i.e. if the index is 0, 64, 128, … then bit 0 is set, if the index is 1, 65, 129, … then bit 1 is set, and so on.

Question: given an index and a count (or a lower and upper index), how can I set the bits for all indexes in this range without using a loop?

  • 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-01T15:17:45+00:00Added an answer on June 1, 2026 at 3:17 pm
    long SetRangeMask(int lower, int upper)     // 3..7
    {
       if (! (lower <= upper)) throw new ArgumentException("...");
    
       int size = upper - lower + 1;            // 7 - 3 + 1 = 5
       if (size >= 64) return -1;
       long mask = (1 << size) - 1;             // #00100000 - 1  = #000011111
       return mask << lower | mask >> -lower;   // #00011111 << 3 = #011111000
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a typical network protocol consisting of typical message stream (32-bit length field
I have used bit field with a structure like this, struct { unsigned int
Bit of an unusual question, but I have setup a field inside a MySQL
I have a bit of a problem that I can't seem to code my
I have a uint32 variable for some bit field flag state and an enum
Question says it all. If I have this for a 96-bit field: uint32_t flags[3];
all tables have a bit field IsDeleted is there a way to tell EF
In a sql table I have a bit field and the value is displayed
I wanted to replace bit/bits (more than one) in a 32/64 bit data field
Let's say i have this bit field value: 10101001 How would i test if

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.