将UNIX时间戳表示为两个数字(前32位和后16位)

时间:2015-12-01 07:40:32

标签: javascript unix-timestamp

我需要将UNIX时间表示为JavaScript中的两个数字 - 前32位和后16位(足够一段时间)。

因此,如果数字可能大于2 ^ 32(但小于2 ^ 48),我希望得到0-31位部分和32-47位部分作为两个数字。获得第一个数字很容易,但第二个数字不是由于JavaScript的位操作符的32位。

我可以做类似

的事情
longNumber.toString(2).substring(0, longNumber.length - 32)

获取第二个数字的二进制值并将其转换为十进制数。但我想知道没有字符串转换可以做到吗?

1 个答案:

答案 0 :(得分:1)

想到这个(它还将前32位分成两个16位):

 #include "stdafx.h"
 #include <iostream>



 using namespace std;




 void calcul(int& nb, int& m, double& sd, double& x_min, int& n, double& y_w);


struct speed
{
   double v_0;
};

struct dat1
{
double x_m;
};

struct dat2
{
double x_l0, x_r0, x_lq, e_l, e_r, rl_h, rb_h, a1, a2, b1;
};


struct sor_arr
{
double x[6500], y[6500], z[6500];
};


struct ef_mat
{
double x_f[35000], y_f[35000];
};




int main()
{
int nb1 = 10;
int m1 = 250;
double sd1 = 16;
double x1nmin = 1.e38;
int n1 = 0;
double yw1 = 0;




calcul(nb1, m1, sd1, x1nmin, n1, yw1);


system("PAUSE");
return 0;
}

void calcul(int& nb, int& m, double& sd, double& x_min, int& n, double& y_w)
{
struct speed sp;
struct dat1 d1;
struct dat2 d2;
struct sor_arr s;
struct ef_mat ef;

int i1;
int i2;
int i3;


double y_m;
double y_ma;
double y_mi;
double x_ma;
double x_mi;
double y_y;
double x_x;
double c1_s;
double x_l;
double x_r;
double cl_r;
double c2_s;
double r_m1;
double x_q;





y_m = y_w - d2.rl_h;
y_ma = d2.rl_h;
y_mi = -d2.rl_h;
x_ma = d2.rb_h;
x_mi = -d2.rb_h;

for (int i = m; i > 1;  i--)
{
    y_y = s.z[i] - y_m;

    if (y_y > y_ma)
    {
        continue;
    }

    if (y_y <= y_ma)
    {
        continue;
    }

    c1_s = s.y[i] / sp.v_0;
    x_l = d2.x_l0 + d2.e_l * pow(c1_s, d2.a1);
    x_r = d2.x_r0 + d2.e_r * pow(c1_s, d2.a2);
    cl_r = x_r - x_l;
    c2_s = s.x[i] + x_min;

    i1 = 1;
    i2 = nb;
    i3 = 1;

    if (fmod(i,2)== 0)
    {


        for (int i = i1; i < i2; i+= i3)
        {
            r_m1 = float(i - 1);
            x_q = c2_s + r_m1 * sd - x_l;
            x_q = x_q * d2.b1 / cl_r + d2.x_lq;
            x_x = x_q - d1.x_m;


            if (x_x >= x_ma)
            {
                continue;
            }

            if (x_x < x_ma)
            {
                continue;
            }

            n = n + 1;
            ef.y_f[n] = y_y;
            ef.x_f[n] = x_x;


        }

        i1 = nb;
        i2 = 1;
        i3 = -1;

    }


   }

}