Storing 2 bytes in one
-
No offense but that's a really bad idea if all he wants to do is map 2D coordinates to 1D coordinates..
-
Hey, i've been looking for an effective method of storing 2 byte values within one. Or, two short values within one. The system I currently use is by embedding an unsafe code to point to a byte value and to cast the relevant data. Here's an example:
byte db, v1, v2;
v1 = 50;
v2 = 80;unsafe
{
byte* db_p = &db;
db_p[0] = (byte)((v1) >> 2);
db_p[1] = (byte)(((v1) << 6) | (((v2) >> 4)&0x3F));
db_p[2] = (byte)((v2) << 4);
}Which stores two values excellently. However, For values below 4 generally, numbers tend to overlap each other. IE, values of
v1 = 0, v2 = 0;
andv1 = 1, v2 = 1;
tend to have the same digit value ofdb
. I was wondering there were any other methods of storing 2 values of data (bytes) within one single data (I'm using this to remove the necessity for multi-dimensional arrays and large collection objects). Thank you!Lets have a simple try at this. If you have a series of X/Y co-ordinates which is assocaited with some data, the simplist method is this:
Dictionary<Point,YourData> dictionary = new Dictionary<Point,YourData>();
where Point is in the System.Drawing namespace (you could define your own if its not fit for your purpose - its just the dictionary key) and YourData is any class or data structure of your choosing containing data assocaited with x/y coords. There sure are many more efficient ways of storing data, but why not get your functionality working and worry about optimization when it becomes a bottleneck.
-
But seriously, for what this poster wants it will probably be entirely workable - and far far far less complex than what he's trying to do. Basically this poster is guilty of premature optimization IMO.
What he is trying to do is flawed,
x + y * width
is far simpler than a dictionary though. Sure it'll work, but that doesn't make it good. edit: the dictionary approach would be better for sparser grids, ok. Otherwise, please no. You would have to check whether a cell exists before trying to use it, instead of just getting the default values (and adding all cells would negate any benefit the dictionary might have had) Compare:array[x + y * width] = value;
Point p = new Point(x, y);
if (dictionary.ContainsKey(p))
dictionary[p] = value;
else
dictionary.Add(p, value);The choice seems clear - assuming of course that we're talking about a non-sparse grid of known and fixed size.
-
Hey, i've been looking for an effective method of storing 2 byte values within one. Or, two short values within one. The system I currently use is by embedding an unsafe code to point to a byte value and to cast the relevant data. Here's an example:
byte db, v1, v2;
v1 = 50;
v2 = 80;unsafe
{
byte* db_p = &db;
db_p[0] = (byte)((v1) >> 2);
db_p[1] = (byte)(((v1) << 6) | (((v2) >> 4)&0x3F));
db_p[2] = (byte)((v2) << 4);
}Which stores two values excellently. However, For values below 4 generally, numbers tend to overlap each other. IE, values of
v1 = 0, v2 = 0;
andv1 = 1, v2 = 1;
tend to have the same digit value ofdb
. I was wondering there were any other methods of storing 2 values of data (bytes) within one single data (I'm using this to remove the necessity for multi-dimensional arrays and large collection objects). Thank you!To quote the late, great Mitch Hedberg when describing 2-in-1 shampoo... 2-in-1 is a BS term, because 1 is not big enough to hold 2. That's why 2 was created. If it was 2 in 1, it would be overflowing... the bottle would be all sticky... To store 2 byte values, 2 bytes are required. To store 2 nibbles (half-bytes), sure you could use 1 byte, but the question begs why?