Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Storing 2 bytes in one

Storing 2 bytes in one

Scheduled Pinned Locked Moved C#
databasetutorial
24 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    No offense but that's a really bad idea if all he wants to do is map 2D coordinates to 1D coordinates..

    J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #21

    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.

    L 1 Reply Last reply
    0
    • C Chris Copeland

      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; and v1 = 1, v2 = 1; tend to have the same digit value of db. 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!

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #22

      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.

      1 Reply Last reply
      0
      • J J4amieC

        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.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #23

        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.

        1 Reply Last reply
        0
        • C Chris Copeland

          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; and v1 = 1, v2 = 1; tend to have the same digit value of db. 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!

          V Offline
          V Offline
          vtchris peterson
          wrote on last edited by
          #24

          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?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups