Standard library / sync/atomic

atomic.AndInt32()

Atomically apply bitwise AND and return the previous value (Go 1.23+). Raw pointer APIs require correctly aligned storage; prefer the corresponding typed atomic in new code. Expected stdout: '12\n8\n'.

Formal Syntax & Structure

go syntaxgo
func AndInt32(addr *int32, mask int32) (old int32)

Parameters & Attributes

NameTypeDescription
addr*int32Pointer to the shared variable; satisfy architecture alignment requirements and use atomic access consistently.
maskint32Bit mask used by the atomic operation.

Return Value

(old int32)

Previous value before AND.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync/atomic")
func main() {  x := new(int32)  atomic.StoreInt32(x, 12)  fmt.Println(atomic.AndInt32(x, 10))  fmt.Println(atomic.LoadInt32(x))}
Go Reference