DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • TypeScript: Useful Features
  • Universal Implementation of BFS, DFS, Dijkstra, and A-Star Algorithms
  • Doubly Linked List in Data Structures and Algorithms
  • Linked List in Data Structures and Algorithms

Trending

  • Logging to Infinity and Beyond: How To Find the Hidden Value of Your Logs
  • Effective Tips for Debugging Complex Code in Java
  • Writing Reusable SQL Queries for Your Application With DbVisualizer Scripts
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  1. DZone
  2. Data Engineering
  3. Data
  4. BitVector32 in .NET

BitVector32 in .NET

Keyvan Nayyeri user avatar by
Keyvan Nayyeri
·
May. 13, 11 · News
Like (0)
Save
Tweet
Share
5.57K Views

Join the DZone community and get the full member experience.

Join For Free
after talking about bitarray class in .net as a lesser-used data collection type designed for improving the efficiency and ease of dealing with bits and small integers, i think it’s also worth talking about a related structure, bitvector32 , located in system.collection.specialized namespace. as the namespace suggests, this is also a specialized collection type that is built with the intention to improve the efficiency of operating on bits and small integers.

bit vector is a known data structure in computer programming for specific applications and in one way or another each programming framework provides a good way to deal with this structure. essentially, bit vectors provide a mechanism for dealing with bits and small integers in a space-efficient manner.

it’s important to emphasize on the fact that unlike bitarray that is a class, bitvector32 is a structure and is built based on another structure, system.collections.specialized.bitvector32.section . bitvector32 has a better efficiency than bitarray as the latter one can grow as much as needed (as i described before ) but the former one has a constant 32 bits storage.

a bitvector32 can be set up in two different ways: using sections for small integers or bit flags for booleans. generally, using the section to work with a bitvector32 requires special attention as a tiny mistake can yield strange results.

bitvector32 provides two constructors:

  • bitvector32(bitvector32) : initiates the structure using the data in another bitvector32 object.
  • bitvector32(int32) : initiates the structure using the data represented by an integer number.

it also provides two properties:

  • data : the value represented by the structure as an integer.
  • item : the value stored in a specific section or mask.

unlike bitarray , bitvector32 provides two methods (with their overloads) only:

  • createmask : creates the first mask in a series.
  • createsection : creates additional masks in a series.

note that bitvector32 is designed specifically for 32 bit integers and there is no available structure for other lengths of integers in the .net framework, however, rob kennedy has implemented his own bitvector64 structure that you can use if you need to have such a structure in your code.

bit flags

one way to use bitvector32 structure is to apply bit flags where you can apply createmask methods to create a series of masks and set specific items to the bit flag that you desire.

in the below example, i create a simple bitvector32 object using a series of masks to build an integer.

private static void bitflags()
02.{
03. // initialize the vector to 2: 00000000 00000000 00000000 00000010
04. bitvector32 bitvector = new bitvector32(2);
05.
06. console.writeline("initially, the value of vector is {0} represented as \n {1}",
07. bitvector.data, bitvector.tostring());
08.
09. int bit1 = bitvector32.createmask();
10. int bit2 = bitvector32.createmask(bit1);
11. int bit3 = bitvector32.createmask(bit2);
12.
13. // set the first bit to true: 00000000 00000000 00000000 00000011
14. bitvector[bit1] = true;
15. console.writeline("setting the first bit to true, the value of vector is {0} represented as \n {1}",
16. bitvector.data, bitvector.tostring());
17.
18. // set the second bit to false: 00000000 00000000 00000000 00000001
19. bitvector[bit2] = false;
20. console.writeline("setting the second bit to false, the value of vector is {0} represented as \n {1}",
21. bitvector.data, bitvector.tostring());
22.
23. // set the third bit to true: 00000000 00000000 00000000 00000101
24. bitvector[bit3] = true;
25. console.writeline("setting the third bit to true, the value of vector is {0} represented as \n {1}",
26. bitvector.data, bitvector.tostring());
27.}

here i initialize my bitvector32 structure to the integer value 2 then create three masks in a sequence and set different bits to customize the structure and get different results.

output

sections

the other way to work with bitvector32 structures is to use bitvector32.section structure. in this approach you build the vector from a set of smaller section from right to left with different values and sizes.

private static void sections()
02.{
03. // initialize the vector to 0: 00000000 00000000 00000000 00000000
04. bitvector32 bitvector = new bitvector32(0);
05.
06. // initialize the first section with 2 bits: 00
07. bitvector32.section section1 = bitvector32.createsection(3);
08. console.writeline("initial value of section 1: {0}", bitvector[section1]);
09.
10. // initialize the second section with 3 bits: 000
11. bitvector32.section section2 = bitvector32.createsection(7, section1);
12. console.writeline("initial value of section 2: {0}", bitvector[section2]);
13.
14. // initialize the third section with 4 bits: 0000
15. bitvector32.section section3 = bitvector32.createsection(11, section2);
16. console.writeline("initial value of section 3: {0}", bitvector[section3]);
17.
18. console.writeline();
19.
20. // set the first section to 3: 11
21. bitvector[section1] = 3;
22. console.writeline("new value of section 1: {0}", bitvector[section1]);
23.
24. // set the second section to 7: 111
25. bitvector[section2] = 7;
26. console.writeline("new value of section 2: {0}", bitvector[section2]);
27.
28. // set the third section to 11: 1011
29. bitvector[section3] = 11;
30. console.writeline("new value of section 3: {0}", bitvector[section3]);
31.
32. console.writeline();
33.
34. // the value of vector is 383: 00000000 00000000 00000001 01111111
35. console.writeline("the value of vector is {0} represented as \n {1}",
36. bitvector.data, bitvector.tostring());
37.}

here i create a new vector initialize to zero then build three sections of different sizes. note that the createsection method is creating a new section with the number of bits represented by the integer not the integer itself, so in the above code i’ve created three sections of 2, 3, and 4 bits length respectively all set to zero initially. in the next step i set each section to a specific integer and the binary representation of that integer will be put in the section. the tricky part is here to avoid using an integer that requires more bits that the section defined which can cause unexpected results. in the last step i print out the vector value that consists of the sections defined for it.

output

conclusion

bitarray and bitvector32 are two lesser-known collection types in the .net framework to deal with small integers and bits where bitvector32 is more efficient as its has a fixed size and bitarray can have an indefinite size.




Data structure Data Types

Published at DZone with permission of Keyvan Nayyeri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • TypeScript: Useful Features
  • Universal Implementation of BFS, DFS, Dijkstra, and A-Star Algorithms
  • Doubly Linked List in Data Structures and Algorithms
  • Linked List in Data Structures and Algorithms

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: