views:

119

answers:

3

How to simulate bit fields in Scala? The bit fields are used to access some bits of one type (like this in C link). I know it's possible to write with bit operators, but I think there a better way if not consider the performance.

Thanks for every hint that might give.

+3  A: 

Sadly not... The shift operators and bitwise boolean operators are pretty much all you've got.

Kevin Wright
With my current knowledge of Scala I don't know how to do, but I think that is possible using some structure of traits. Thanks for the reply :)
adelarsq
+6  A: 

If you just want single bits, then collection.BitSet will work for you.

If you want a proper bit field class, then you're out of luck for two reasons. First, because Scala doesn't have one. Second, because even if it did, the space savings would probably not be very impressive since the overhead of the enclosing object would probably be large compared to your bits.

There are a couple of ways out of this with some work: a custom-defined class that wraps an integer and lets you operate on parts of it as bit fields; when you go to store the integer, though, you just have it saved as a primtive int. Or you could create an array of bit field structs (of arbitrary length) that are implemented as an array of integers. But there's nothing like that built in; you'll have to roll your own.

Rex Kerr
+1  A: 

There's also this repo,

word-aligned compressed variant of the Java bitset class. It uses a 64-bit run-length encoding (RLE) compression scheme.

http://code.google.com/p/javaewah/

Gene T