开发者

Can someone give me a real world example for when I might want/need to use the BitArray class in .NET?

开发者 https://www.devze.com 2023-01-09 21:24 出处:网络
I understand how to开发者_JS百科 use the BitArray class, but am looking for an instance where one could put it to use in a real world program.The most valid use of BitArray I\'ve seen is using it as t

I understand how to开发者_JS百科 use the BitArray class, but am looking for an instance where one could put it to use in a real world program.


The most valid use of BitArray I've seen is using it as the backing store for a group of bool values in a size sensitive class.

class Example { 
  private BitArray _data = new BitArray(...);
  public bool Value0 { 
    get { return _data.Get(0); }
    set { _data.Set(0,value); }
  }
  public bool Value1 {
    get { return _data.Get(1); }
    set { _data.Set(1,value); }
  }
  ...

Using a backing 'bool' for every field above would create an overhead of 1 byte per property. Using a BitArray has the potential for reducing the overhead to 1 bit per property in the best case scenario. This can be significant for certain specific cases.

Note: When I say valid above I mean a case where the sensitive of the size of the type is not guessed but is actually measured and proved to be significant.


Sure Bit Array Collection Use

If you needed to use it you would know, but we used it to decode radar measurements. We basically we receiving a message and needed to decode it piece by piece using bit maniupluation. With that being said... we could have used something else it was just any easy plugin, for what we were doing.


Bit arrays have been used in 3D games such as Quake (and possibly Doom, can't recall) to represent all the surfaces that are visible from a given location (floor sector).

It works something like this: Given a complex 3D scene, number all the surfaces (triangles) sequentially. The count of surfaces is the size of the bit vector assigned to every floor sector. For each floor sector (region of the floor map), use ray tracing to determine which surfaces are visible from that floor sector. If a surface is visible, set it's corresponding bit in the floor sector's bit array to 1 (floorsector.bitarray[surface_index] = 1).

This bit array per floor sector can be precalculated when the level is built, saving a significant amount of CPU time when running the game. When the player/viewpoint is in a given sector and facing a particular direction, the rendering engine need only consider the surfaces marked in the floor sector's bit array. Since the view camera (frustrum) is looking only in one direction at a time, only a portion of the surfaces marked in the floor sector's bit array will actually need to be rendered on the screen, but the bit array reduces the number of surfaces that need to be tested at runtime by several orders of magnitude.

A complex, large game level may have tens of millions of distinct surface tiles, but only a few thousand surface tiles are visible from any given location (floor sector) in the level. Furthermore, the number of surfaces visible from a given location tends to remain about the same regardless of how large the level is. This technique of precalculating surface visibility essentially enables arbitrarily huge game levels with little or no impact on realtime rendering.

Since these visibility bit arrays tend to be "clumpy" (1 bits not distributed uniformly across the array), the arrays can be compressed further for storage on disk using "zero compression" - RLE compression to drop all the zero bytes.

This precalculated surface visibility bit array technique is what makes huge levels possible in Quake and other more recent games, compared to earlier rendering engines in, say, Castle Wolfenstein where the levels had to be kept relatively small to avoid bogging down the renderer at runtime.

Storing the visible surface indices as a bit vector and using zero compression on top of that results in massive compression of a large amount of sparse data.


I would think communicating with embedded devices could make this handy, they don't always send data in 4 or 8 bit structures.


I have to scan an array that has some 100k elements REPEATEDLY. Upon scanning, I find some ranges in the array that I should not scan again. Marking the elements and ranges that are to be excluded from the further scans were most easily done by using BitArray class. It's a shame that it doesn't support SetRange(int low, int high, bool value), but I implemented it manually using a simple loop - depending on a internal representation (I guess that they use uint[]) it could be done faster.

0

精彩评论

暂无评论...
验证码 换一张
取 消