Atomics.and()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since December 2021.

Die statische Methode Atomics.and() berechnet einen bitweisen UND mit einem gegebenen Wert an einer bestimmten Position im Array und gibt den alten Wert an dieser Position zurück. Diese atomare Operation garantiert, dass keine andere Schreiboperation stattfindet, bis der modifizierte Wert zurückgeschrieben wird.

Probieren Sie es aus

// Create a SharedArrayBuffer with a size in bytes
const buffer = new SharedArrayBuffer(16);
const uint8 = new Uint8Array(buffer);
uint8[0] = 7;

// 7 (0111) AND 2 (0010) = 2 (0010)
console.log(Atomics.and(uint8, 0, 2));
// Expected output: 7

console.log(Atomics.load(uint8, 0));
// Expected output: 2

Syntax

js
Atomics.and(typedArray, index, value)

Parameter

typedArray

Ein Integer-typisiertes Array. Eines von Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array oder BigUint64Array.

index

Die Position im typedArray, an der der bitweise UND berechnet werden soll.

value

Die Zahl, mit der der bitweise UND berechnet werden soll.

Rückgabewert

Der alte Wert an der angegebenen Position (typedArray[index]).

Ausnahmen

TypeError

Wird ausgelöst, wenn typedArray nicht einer der erlaubten Integer-Typen ist.

RangeError

Wird ausgelöst, wenn index außerhalb der Grenzen des typedArray liegt.

Beschreibung

Der bitweise UND-Operator ergibt nur dann 1, wenn sowohl a als auch b 1 sind. Die Wahrheitstabelle für die UND-Operation ist:

a b a & b
0 0 0
0 1 0
1 0 0
1 1 1

Zum Beispiel ergibt ein bitweises UND von 5 & 1 das Ergebnis 0001, was dezimal 1 ist.

5  0101
1  0001
   ----
1  0001

Beispiele

Nutzung von and()

js
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
ta[0] = 5;

Atomics.and(ta, 0, 1); // returns 5, the old value
Atomics.load(ta, 0); // 1

Spezifikationen

Specification
ECMAScript® 2026 Language Specification
# sec-atomics.and

Browser-Kompatibilität

Siehe auch