Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Storing two numbers in an integer.
- //http://forum.unity3d.com/threads/149073-PAID-Junior-Unity-developer-required-Full-time?p=1021224&viewfull=1#post1021224
- // I quite like:
- var i = int.Parse(Console.ReadLine()) << 16;
- i |= 65535 & int.Parse(Console.ReadLine());
- Console.WriteLine((i >> 16));
- Console.WriteLine((i << 16) >> 16);
- //Or if you are afraid of bitwise operators:
- var i = BitConverter.ToInt32(
- BitConverter.GetBytes(short.Parse(Console.ReadLine()))
- .Concat(
- BitConverter.GetBytes(short.Parse(Console.ReadLine()))).ToArray(),
- 0);
- Console.WriteLine(BitConverter.ToInt16(BitConverter.GetBytes(i), 0));
- Console.WriteLine(BitConverter.ToInt16(BitConverter.GetBytes(i), 2));
- //Or if you like variable length data:
- //http://stackoverflow.com/questions/11979184/reading-bit-aligned-data/11979287#11979287
- //The following is useful for large quantities of data:
- var i = new int[1];
- Buffer.BlockCopy(new[] { short.Parse(Console.ReadLine()), short.Parse(Console.ReadLine()) }, 0, i, 0, 4);
- var s = new short[2];
- Buffer.BlockCopy(i, 0, s, 0, 4);
- Console.WriteLine(s[0]);
- Console.WriteLine(s[1]);
- //Oh, and here's a trick I bet they never taught you in school:
- var mi = new MyInteger { a = short.Parse(Console.ReadLine()), b = short.Parse(Console.ReadLine()) };
- var mi2 = new MyInteger { i = mi.i };
- Console.WriteLine(mi2.a);
- Console.WriteLine(mi2.b);
- ...
- [StructLayout(LayoutKind.Explicit)]
- struct MyInteger
- {
- [FieldOffset(0)]
- public int i;
- [FieldOffset(0)]
- public short a;
- [FieldOffset(2)]
- public short b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement