Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The | operator in Go is the bitwise OR operator. It performs a logical inclusive OR operation on each pair of corresponding bits of two integer operands. If either or both bits in the compared position are 1, the resulting bit is 1; otherwise, it evaluates to 0.
result := operand1 | operand2

Bit-Level Evaluation

The operator evaluates the binary representation of the operands using the following truth table for each bit position:
Bit ABit BResult (A | B)
000
011
101
111

Type Constraints

In Go, the | operator is strictly constrained to integer types (e.g., int, uint8, int32, byte, rune).
  • It cannot be applied to floating-point numbers (float32, float64), complex numbers, or strings.
  • It cannot be used for boolean logic; Go uses the || operator for logical OR operations between bool types.
  • Both operands must resolve to the same integer type. If the operands are typed variables of different integer types, an explicit type conversion (e.g., uint8(b)) is required prior to the operation. However, an untyped integer constant (e.g., 12) can be used directly with a typed integer variable, as the Go compiler implicitly converts the untyped constant to the variable’s type.

Mechanical Example

The following example demonstrates how the | operator processes the underlying binary values of two uint8 integers.
package main

import "fmt"

func main() {
    var a uint8 = 10 // Binary representation: 00001010
    var b uint8 = 12 // Binary representation: 00001100

    // Bitwise OR operation
    result := a | b  // Binary representation: 00001110 (Decimal: 14)

    fmt.Printf("a:      %08b\n", a)
    fmt.Printf("b:      %08b\n", b)
    fmt.Printf("result: %08b\n", result)
}
Execution Trace:
  00001010  (a)
| 00001100  (b)

  00001110  (result)

Compound Assignment

Go also supports the bitwise OR assignment operator (|=), which applies the bitwise OR operation to a variable and assigns the result back to that same variable in a single statement.
var a uint8 = 10
var b uint8 = 12

a |= b // Syntactic sugar for: a = a | b
Master Go with Deep Grasping Methodology!Learn More