Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
C# Reference
C# Operators
 & Operator

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Language Reference
& Operator (C# Reference)

The & operator can function as either a unary or a binary operator.

The unary & operator returns the address of its operand (requires unsafe context).

Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

The & operator evaluates both operators regardless of the first one's value. For example:

int i = 0;
if (false & ++i == 1)
{
    // i is incremented, but the conditional
    // expression evaluates to false, so
    // this block does not execute.
}

User-defined types can overload the binary & operator (see operator). Operations on integral types are generally allowed on enumeration. When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded.

// cs_operator_ampersand.cs
using System;
class MainClass
{
    static void Main() 
    {
        Console.WriteLine(true & false); // logical and
        Console.WriteLine(true & true);  // logical and
        Console.WriteLine("0x{0:x}", 0xf8 & 0x3f);  // bitwise and
    }
}

Output

False
True
0x38

Reference

C# Operators

Concepts

C# Programming Guide

Other Resources

C# Reference

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker