Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 莫顿码的计算_C++_Math_Bit Manipulation_64 Bit_Z Order Curve - Fatal编程技术网

C++ 莫顿码的计算

C++ 莫顿码的计算,c++,math,bit-manipulation,64-bit,z-order-curve,C++,Math,Bit Manipulation,64 Bit,Z Order Curve,我正在尝试(为了计算莫顿码)将2个有符号的长数字(如x和y(32位))与值交错 案例1: x = 10; //1010 y = 10; //1010 结果将是: 11001100 案例2: x = -10; y = 10; 二进制表示是 x = 1111111111111111111111111111111111111111111111111111111111110110 y = 1010 对于交织,我只考虑32位表示,其中我可以将x的31位与y的31位交织, 使用以下代

我正在尝试(为了计算莫顿码)将2个有符号的长数字(如
x
y
(32位))与值交错

案例1:

x = 10;  //1010
y = 10;  //1010
结果将是:

11001100
案例2:

   x = -10;
   y = 10;
二进制表示是

x = 1111111111111111111111111111111111111111111111111111111111110110
y = 1010
对于交织,我只考虑32位表示,其中我可以将
x
的31位与
y
的31位交织, 使用以下代码

signed long long x_y;
for (int i = 31; i >= 0; i--)
        {

                    unsigned long long xbit = ((unsigned long) x)& (1 << i);
                    x_y|= (xbit << i);

                    unsigned long long ybit = ((unsigned long) y)& (1 << i);

                    if (i != 0)
                    {
                                x_y|= (x_y<< (i - 1));
                    }
                    else
                    {
                                (x_y= x_y<< 1) |= ybit;
                    }
        }
有符号长x_y;
对于(int i=31;i>=0;i--)
{

unsigned long xbit=((unsigned long)x)和(1我认为下面的代码符合您的要求

莫顿码是64位的,我们用两个32位的数字交错生成64位的数字。 由于数字已经签名,我们必须考虑负数为,

if (x < 0) //value will be represented as 2's compliment,hence uses all 64 bits
    {
        value = x; //value is of 32 bit,so use only first lower 32 bits 
        cout << value;
        value &= ~(1 << 31); //make sign bit to 0,as it does not contribute to real value.
    }
我希望这有帮助。:)

unsigned long long x_y_copy = 0; //make a copy of ur morton code
//looping for each bit of two 32 bit numbers starting from MSB.
    for (int i = 31; i >=0; i--)
    {
        //making mort to 0,so because shifting causes loss of data
        mort = 0;
        //take 32 bit from x
        int xbit = ((unsigned long)x)& (1 << i);
        mort = (mort |= xbit)<<i+1;    /*shifting*/
        //copy  formed code to copy ,so that next time the value is preserved for appending
        x_y_copy|= mort;
         mort =0;
        //take 32nd bit from 'y' also
        int ybit = ((unsigned long)y)& (1 << i);
        mort = (mort |= ybit)<<i;
        x_y_copy |= mort;
    }
    //this is important,when 'y'  is negative because the 32nd bit of 'y' is set to 0 by above first code,and while moving 32 bit of 'y' to morton code,the value 0 is copied to 63rd bit,which has to be made to 1,as sign bit is not 63rd bit.
    if (mapu_y < 0)
    {
        x_y_copy = (x_y_copy) | (4611686018427387904);//4611686018427387904 = pow(2,63)
    }