views:

159

answers:

3

How about a Nibble etc.

+10  A: 

No. Even if you have an array of Booleans, I believe they're specified to take up one byte each.

Of course you can define your own data types which have fewer than 256 valid values (like Boolean does) but you can't make it take up less than a byte in memory.

As LBushkin pointed out, there are types such as BitArray and BitVector32 which effectively pack multiple bits efficiently - you could write your own NybbleArray type if you wanted.

Jon Skeet
So if I would want to define a Nibble type I should do it with Byte, right, since it's still better than array of 4 booleans...
Shimmy
@Shimmy: Yes, that's right.
Jon Skeet
This table is better since it's tells you the size of each type, unlike the first one :) http://msdn.microsoft.com/en-us/library/47zceaw7.aspx
Shimmy
@Jon, so, should I use a BitArray or a single byte?
Shimmy
I just took a look into the reflected BitArray, it's just a Boolean array with some functionality, for performance and portability it's not the best idea.
Shimmy
@Shimmy: Hmm... it's possible that I'm wrong about the space taken up by a boolean array, of course...
Jon Skeet
@Shimmy: I don't think that's correct. Looking at the reflected code myself, I see it is implemented as an `int[]` array, with each `int` value serving as its own packed group of bits.
Dan Tao
@Dan, right, so anyway, it's definitely larger than a single Byte implementation (at least 4 bytes), so conclusion is, if you want to implement a Nibble, then it should probably be implemented with underlying Byte or SByte for best performance capabilities (don't forget it's a value type), or if you need it to play with single Nibbles, then I would better suggest to use the existing BitArray or BitConverter shared (static) methods or implement another shared class with your own functionality, or just embed it all in your byte-implemented Nibble structure, which is what I am going to do..
Shimmy
@Shimmy: I only meant to point out that it's designed in a way that makes sense -- i.e., what you would expect from a name like `BitArray` -- as opposed to "a Boolean array" as you described it, which would make it a pretty pointless data structure. As for what makes the most sense for performance capabilities, I'd say that depends entirely on your needs. Yes, if you want exactly 8 bits in the form of an immutable struct then sure, a `byte` makes sense. If you want 32 bits, I'd say an `int` makes more sense than 4 `byte`s. `BitArray` is mutable and intended to be used as such.
Dan Tao
@Shimmy: In any case, if your intention is specifically to create a data type that is *smaller* than a `byte`, the purpose of these answers is to point out that this isn't possible; you can only store data smaller than 1 `byte` if you break up existing structures (`byte`, `int`, or otherwise) in such a way that they represent multiple pieces of information.
Dan Tao
Right, but I want portable immutable objects that are less than a byte and not grouped, I guess I will simply use implementation of a Byte wrapper and that's it, cuz that's the smallest amount possible (in memory). thanks.
Shimmy
Is there a way to generate a compiler error if I try to set for the nibble any value higher than 0xF?
Shimmy
@Shimmy: No, I don't believe so.
Jon Skeet
+1  A: 

No, byte is the smallest.

This may be helpful: http://stackoverflow.com/questions/3124960/how-can-you-nibble-nybble-bytes-in-c

Mandelbrot
Doesn't a boolean take less?
Shimmy
Less memory? No. Fewer possible values? Yes. If you are trying to implement a Nibble type then the best way would be to either use a BitArray/BitVector as previously mentioned or to use bitmasking on bytes.
Mandelbrot
+4  A: 

There's no native data-type smaller than byte, however if you want to store and manipulate a group of packed bits, you can use BitVector32 or BitArray.

LBushkin
I think I am gonna use simple Byte, since BitVector is 4 bytes (Int32) length (at least).
Shimmy