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

The Archive Base Latest Questions

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

I have a couple of classes BitMask & BitMaskLarge that both implement an interface

  • 0

I have a couple of classes BitMask & BitMaskLarge that both implement an interface IBitMask that exposes functionality to perform bitwise logic operations, etc. The BitMask class contains the a private _mask field of type long and the BitMaskLarge class contains a private _masks array of longs.

The end user works with IBitMask and the implementation details of each of the classes should be transparent to them, such as how the bitwise logic is implemented when both inputs are of one type as opposed to of one type each.

When creating an IBitMask a BitMaskFactory is used that returns the appropriate type, based on the bitwidth passed into the constructor.

The problem occurs when setting a bit in the bit mask using the SetBitIndex method. If the bit is outside the bounds of the current mask, BitMaskLarge simply adds another element to _masks and sets the appropriate bit. However BitMask cannot go beyond 64 bits and so need to convert to a BitMaskLarge. Obviously this is impossible when calling bitmask.SetBitIndex(100) on the BitMask class, which should set the bit at index 100.

One possible solution I can think of would be to make SetBitIndex internal, and to create a static method on the BitMaskFactory that returned the new or updated IBitMask. This is not ideal as it is not natural to use. If anyone has a better solution to this problem, I’d like to hear it. All ideas are welcome, no matter how fundamental the changes required.

  • 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-18T23:55:10+00:00Added an answer on May 18, 2026 at 11:55 pm

    You can make types implementing IBitMask immutable, and have mutating operations return a new instance. This would run like this:

    IBitMask mask = BitMaskFactory(...); // assume mask is now a BitMask
    mask = mask.SetBitIndex(100); // now mask is a BitMaskLarge
    

    Making the instances immutable also offers thread safety “for free”.

    If you find this inconvenient, you can solve it by adding another layer of abstraction:

    class BitMaskImplementation : IBitMask {
        private IBitMask mask;
    
        // BitMaskImplementation defers all operations to mask,
        // and you can change the object stored inside mask without
        // your callers knowing anything about what happened.
    }
    

    This loses you the thread safety though.

    Update:

    You shouldn’t really go to all this trouble unless a BitMaskLarge is much more expensive for you than a BitMask, and you also expect nearly all masks to be simple BitMasks.

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

Sidebar

Related Questions

I have a need to create a couple of classes that will serve as
I have a couple classes extending builtin datetime.* Is there any good reason to
I have a couple of classes (for now) and I'm trying to clear up
I have an assignment and we have a couple of classes given, one of
I have a situation where I have a couple of case classes where all
I am using Doctrine 1.2 & ZF 1.10. I have my Doctrine CLI setup
I have Googled and read for hours now and I can't find anyone that
If I have two static variables in different compilation units, then their initialization order
I've run into a nhibernate dilemma that I'm sure can be solved, cause 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.