Bitwise AND Assignment (&=) Expression

Summary

Apply a logical AND 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 AND 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 AND 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 AND Operations Work

In a bitwise AND 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 AND operation will compare each bit. If both bits are 1 (one), the resulting bit is 1 (one); otherwise, the resulting bit is 0 (zero).

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

Differences from JavaScript

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

Examples

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

See Also

Share

HTML | BBCode | Direct Link