CKTIS / JavaScript
Int32Array: interi con segno a 32 bit
Int32Array memorizza numeri come interi con segno a 32 bit da -2147483648 a 2147483647, con 4 byte per elemento.
Versione: ECMAScript 2015 onward; example target: Node.js 24.22.2
Sintassi
new Int32Array(); new Int32Array(length); new Int32Array(source); new Int32Array(buffer[, byteOffset[, length]])new Int32Array(source?, byteOffset?, length?) crea un typed array numerico da lunghezza, iterable/array-like, altro typed array numerico o ArrayBuffer/SharedArrayBuffer. La forma con buffer condivide i byte senza copia, byteOffset deve essere multiplo di 4 e BYTES_PER_ELEMENT vale 4.
L’assegnazione applica ToNumber, tronca la frazione, riduce modulo 2^32 e interpreta il risultato con segno. Quindi 2147483648 diventa -2147483648, 4294967295 diventa -1, e NaN e infiniti diventano 0. Assegnare BigInt o usare un typed array BigInt come sorgente genera TypeError. L’ordine dei byte è nativo; usa DataView per endianness esplicito.
La lunghezza usa ToIndex: le frazioni sono troncate, NaN/undefined diventano 0 e -0.5 dà 0. Risultato negativo dopo ToIntegerOrInfinity, infinito, offset disallineato o intervallo fuori buffer genera RangeError; buffer scollegato genera TypeError. Il tipo è standard da ECMAScript 2015.
Esempi
const values = new Int32Array([2147483647, 2147483648, 4294967295, -2147483649, 3.9, NaN, Infinity]);
console.log(Array.from(values).join(","));
console.log(Int32Array.BYTES_PER_ELEMENT, new Int32Array(2.9).length, new Int32Array(-0.5).length);
const buffer = new ArrayBuffer(12);
const view = new Int32Array(buffer, 4, 1);
view[0] = -123456;
console.log(view.byteOffset, view.byteLength, view[0]);
try { view[0] = 1n; } catch (error) { console.log(error instanceof TypeError); }Risultato
2147483647,-2147483648,-1,2147483647,3,0,0 4 2 0 4 4 -123456 true