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 = bigInt // cut of the first part (with &)?
console.log(highInt); // 0xfff
console.log(lowInt); // 0xfff
// set them together again
var reBigInt = (highInt << 8) + lowInt;
Unfortunately neither getting the highInt nor getting the lowInt works... Could somebody give me the answer how I need to use the bitwise operators?
regards
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.