Bit fields in Scala

How to simulate bit fields in Scala? Bit fields are used to access some bits of the same type (for example, in the C link ). I know that you can write with bitwise operators, but I think there is a better way if you don't take into account performance.

Thanks for every hint that can give.

+6
bit-manipulation scala
source share
3 answers

If you only need individual bits, then collection.BitSet will work for you.

If you need the right class of bit fields, you're out of luck for two reasons. Firstly, because Scala does not have one. Secondly, because even if that were the case, the space savings would probably not be very impressive, since the overhead of the wrapper object would probably be large compared to your bits.

There are several ways out of this with some work: a custom class that wraps an integer and allows you to work with its parts as bit fields; when you go to save an integer, you just saved it as a primitive int. Or you can create an array of bitfield structures (of arbitrary length) that are implemented as an array of integers. But there was nothing of the sort; you have to collapse yourself.

+8
source share

Sorry ... Shift operators and bitwise logical operators are almost everything you have.

+2
source share

There is also this repo,

a word-tight version of the Java bit class. It uses a 64-bit encoding (RLE) compression scheme.

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

+2
source share

All Articles