how to split 64bit integer to two 32bit integers

前端 未结 2 993
夕颜
夕颜 2021-01-02 08:05

I want to split a 64bit integer into two 32bit integers:

var bigInt = 0xffffff;

var highInt = bigInt >> 8 // get the high bits 0xfff
var lowInt = bigI         


        
2条回答
  •  情话喂你
    2021-01-02 08:46

    EDIT JavaScript represents integers using IEEE double precision format, so there is no way to store arbitrary 64 bit integers without loss of precision, except through custom big integer libraries. Bitwise operations on potentially cropped values obviously make no sense.


    In general, for languages that do support 64 bit integers:

    A 64-bit pattern of ones is 0xffffffffffffffff. To extract the upper 32 bits, you need to shift by 32: >> 32. To extract the lower 32 bit, just and them with 32 ones: & 0xffffffff.

    You got the principle right - your arithmetic on how many bits to shift or mask is just wrong.

提交回复
热议问题