Bitwise OR Assignment (|=) Expression

Summary

Apply logical inclusive OR to the value of a variable.

Syntax

variable |= expression

Parameters

variable
The variable, property, or array element to update.
expression
Any legal expression.

Description

For numeric and Boolean variables, the |= operator computes the value of the expression on the right, and then applies a logical inclusive OR operation with it to the variable, property, or array element on the left. The expression on the right is evaluated prior to assignment.

The |= operator is equivalent to the | operator with the left-hand side being the variable, property, or array element to modify and the right-hand side being the expression to apply the logical inclusive OR operation with. Therefore, the following expressions are equivalent:

1
2
x |= 5;    // Equivalent to x = x | 5
x = x | 5; // Equivalent to x |= 5

How Bitwise OR Operations Work

In a bitwise OR operation, values are converted to equal-length binary representations. For example, the decimal (base 10) number 0 (zero) may be converted to the binary form 0000 and the number 1 (one) may be converted to 0001. The bitwise OR operation will compare each bit. If both bits are 0 (zero), the resulting bit is 0 (zero); otherwise, the resulting bit is 1 (one).

1
2
3
4
  0000 (0)
  0001 (1)
  ----
= 0001 (1)
1
2
3
4
  0011 (3)
  0101 (5)
  ----
= 0111 (7)

Differences from JavaScript

Logical inclusive OR operations are not limited to 32-bit integers in JS++. Unimplemented In JavaScript, the logical inclusive OR operation is limited to signed 32-bit integers.

Examples

Basic Usage
1
2
3
4
5
6
7
8
import System;
 
int x = 0;
Console.log(x); // 0
x |= 0;
Console.log(x); // 0
x |= 1;
Console.log(x); // 1

See Also

Share

HTML | BBCode | Direct Link