using System;
class Program
{
public static void Main()
{
Console.WriteLine("IntPtr.Size: {0}", IntPtr.Size);
Console.WriteLine();
// Two rounds, with positive and with negative numbers
long test1 = 0x12340000 /* y */ | 0x00005678 /* x */;
long test2 = 0xDCBA0000 /* y */ | 0x00009876 /* x */;
foreach (long temp in new[] { test1, test2})
{
long test = temp;
if (IntPtr.Size == 8)
{
// We "pad" the number so that all the 64 bits are full and a
// standard (int)xy conversion will fail.
test = unchecked((long)((ulong)test | 0xFFFFFFFF00000000ul));
}
IntPtr xy = (IntPtr)test;
int x = unchecked((short)(long)xy);
int y = unchecked((short)((long)xy >> 16));
// Other way, more similar to WinAPI
ushort loword = unchecked((ushort)(ulong)xy);
ushort hiword = unchecked((ushort)((ulong)xy >> 16));
int x2 = unchecked((int)(short)loword);
int y2 = unchecked((int)(short)hiword);
Console.WriteLine("xy: {0:X}", unchecked((ulong)(long)xy));
Console.WriteLine("x: {0:X}, decimal: {1}, other way: {2:X}, ushort other way: {3:X}", x, x, x2, unchecked((ushort)x2));
Console.WriteLine("y: {0:X}, decimal: {1}, other way: {2:X}, ushort other way: {3:X}", y, y, y2, unchecked((ushort)y2));
Console.WriteLine();
}
}
}
dXNpbmcgU3lzdGVtOwogCmNsYXNzIFByb2dyYW0KewogICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgewogICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJJbnRQdHIuU2l6ZTogezB9IiwgSW50UHRyLlNpemUpOwogICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCk7CgogICAgICAgIC8vIFR3byByb3VuZHMsIHdpdGggcG9zaXRpdmUgYW5kIHdpdGggbmVnYXRpdmUgbnVtYmVycyAgICAgICAgCiAgICAgICAgbG9uZyB0ZXN0MSA9IDB4MTIzNDAwMDAgLyogeSAqLyB8IDB4MDAwMDU2NzggLyogeCAqLzsKICAgICAgICBsb25nIHRlc3QyID0gMHhEQ0JBMDAwMCAvKiB5ICovIHwgMHgwMDAwOTg3NiAvKiB4ICovOwogICAgICAgIAogICAgICAgIGZvcmVhY2ggKGxvbmcgdGVtcCBpbiBuZXdbXSB7IHRlc3QxLCB0ZXN0Mn0pCiAgICAgICAgewogICAgICAgICAgICBsb25nIHRlc3QgPSB0ZW1wOwoKICAgICAgICAgICAgaWYgKEludFB0ci5TaXplID09IDgpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vIFdlICJwYWQiIHRoZSBudW1iZXIgc28gdGhhdCBhbGwgdGhlIDY0IGJpdHMgYXJlIGZ1bGwgYW5kIGEgCiAgICAgICAgICAgICAgICAvLyBzdGFuZGFyZCAoaW50KXh5IGNvbnZlcnNpb24gd2lsbCBmYWlsLgogICAgICAgICAgICAgICAgdGVzdCA9IHVuY2hlY2tlZCgobG9uZykoKHVsb25nKXRlc3QgfCAweEZGRkZGRkZGMDAwMDAwMDB1bCkpOwogICAgICAgICAgICB9CiAgICAgCiAgICAgICAgICAgIEludFB0ciB4eSA9IChJbnRQdHIpdGVzdDsKICAgICAgICAgICAgCiAgICAgICAgICAgIGludCB4ID0gdW5jaGVja2VkKChzaG9ydCkobG9uZyl4eSk7CiAgICAgICAgICAgIGludCB5ID0gdW5jaGVja2VkKChzaG9ydCkoKGxvbmcpeHkgPj4gMTYpKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIE90aGVyIHdheSwgbW9yZSBzaW1pbGFyIHRvIFdpbkFQSQogICAgICAgICAgICB1c2hvcnQgbG93b3JkID0gdW5jaGVja2VkKCh1c2hvcnQpKHVsb25nKXh5KTsKICAgICAgICAgICAgdXNob3J0IGhpd29yZCA9IHVuY2hlY2tlZCgodXNob3J0KSgodWxvbmcpeHkgPj4gMTYpKTsKICAgICAKICAgICAgICAgICAgaW50IHgyID0gdW5jaGVja2VkKChpbnQpKHNob3J0KWxvd29yZCk7CiAgICAgICAgICAgIGludCB5MiA9IHVuY2hlY2tlZCgoaW50KShzaG9ydCloaXdvcmQpOwoKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoInh5OiB7MDpYfSIsIHVuY2hlY2tlZCgodWxvbmcpKGxvbmcpeHkpKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIng6IHswOlh9LCBkZWNpbWFsOiB7MX0sIG90aGVyIHdheTogezI6WH0sIHVzaG9ydCBvdGhlciB3YXk6IHszOlh9IiwgeCwgeCwgeDIsIHVuY2hlY2tlZCgodXNob3J0KXgyKSk7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJ5OiB7MDpYfSwgZGVjaW1hbDogezF9LCBvdGhlciB3YXk6IHsyOlh9LCB1c2hvcnQgb3RoZXIgd2F5OiB7MzpYfSIsIHksIHksIHkyLCB1bmNoZWNrZWQoKHVzaG9ydCl5MikpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgpOwogICAgICAgIH0KICAgIH0KfQ==
IntPtr.Size: 4
xy: 12345678
x: 5678, decimal: 22136, other way: 5678, ushort other way: 5678
y: 1234, decimal: 4660, other way: 1234, ushort other way: 1234
xy: FFFFFFFFDCBA9876
x: FFFF9876, decimal: -26506, other way: FFFF9876, ushort other way: 9876
y: FFFFDCBA, decimal: -9030, other way: FFFFDCBA, ushort other way: DCBA