Search code examples
c#bitarraybitvector

Bitvector32 and Bitarray in C#


I wanted to know whether there are bitwise operators for Bitvector32 that operate in O(1) time. I am currently using BitArray of large sizes and using Bitwise And,Or and Not which operate in O(size of bitarray).

I searched the internet for this but could not find an answer. Hope people here can help!


Solution

    • Converting a BitVector32 to an int is an O(1) operation. (reference)
    • Converting an int to a BitVector32 is an O(1) operation. (reference)
    • Executing a bitwise operation on an int is an O(1) operation.

    Thus,

    var vectorAnd = new BitVector32(vector1.Data & vector2.Data);
    var vectorOr = new BitVector32(vector1.Data | vector2.Data);
    var vectorNot = new BitVector32(~vector1.Data);
    

    are all O(1) operations.