Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

C# 3 byte Ints

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 0.77 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. [StructLayout(LayoutKind.Explicit)]
  2. struct OddUnion
  3. {
  4.     /* The 32-bit integer value */
  5.     [FieldOffset(0)]
  6.     public int IntegerValue;
  7.  
  8.     /* The bytes that overlap with it */
  9.     [FieldOffset(0)]
  10.     public byte Byte1;
  11.     [FieldOffset(1)]
  12.     public byte Byte2;
  13.     [FieldOffset(2)]
  14.     public byte Byte3;
  15.     [FieldOffset(3)]
  16.     public byte Byte4;
  17.  }
  18.        
  19. OddUnion myOddUnion;
  20. myOddUnion.IntegerValue = 4096;
  21. Byte secondByte = myOddUnion.Byte1;
  22.        
  23. int  binary
  24. 0                       00000000
  25. 1                       00000001
  26. ...  
  27. 127                     01111111
  28. 128            00000001 10000000
  29. 129            00000001 10000001
  30. ...
  31. 255            00000001 11111111
  32. 256            00000010 10000000
  33. ...
  34. 16383          01111111 11111111
  35. 16384 00000001 10000000 10000000