#1,107 – The Bitwise AND Operator

You can use the & operator to do a bitwise AND operation between two integer-based values.

An AND operation is applied to two different bits, returning true if both input bits are true.  Here is a truth table showing the output value for all possible input combinations.

  • 0 & 0 = 0
  • 0 & 1 = 0
  • 1 & 0 = 0
  • 1 & 1 = 1

You can use the & operator on two arbitrary integer values as shown below.  The AND operation will be applied to the two integer values, one bit at a time.

            int n1 = 12;
            int n2 = 10;
            int result = n1 & n2;
            Console.WriteLine("{0} & {1} = {2}", n1, n2, result);

1107-001
It may help to use hex notation in order to better understand how the AND operation works on each bit.

            int n1 = 0xC;  // 12 (dec) or 1100 (bin)
            int n2 = 0xA;  // 10 (dec) or 1010 (bin)

            // result = 1000
            int result = n1 & n2;

1107-002

#1,106 – Using the Logical Exclusive OR Operator

You can use the exclusive OR (^) operator to do a bitwise exclusive OR operation.  The operands in this case are integer-based types.

You can also use the exclusive OR operator on boolean operands.  The result of the operation is true if exactly one of the operands (but not both) is true.

  • false ^ false => false
  • false ^ true => true
  • true ^ false => true
  • true ^ true => false
            bool itsThursday = DateTime.Now.DayOfWeek == DayOfWeek.Thursday;
            bool itsMay = DateTime.Now.Month == 5;

            // Wear red shirt in May, wear red shirt every Thursday,
            // but don't wear red shirt on Thursdays in May
            bool wearRedShirt = itsThursday ^ itsMay;

            Console.WriteLine("Thursday:{0}, May:{1}, RedShirt:{2}", itsThursday, itsMay, wearRedShirt);

1106-001

#1,105 – Re-Declaring a Class-Level Variable within a Method

You can re-declare a class-level variable within a method, that is–declare a local variable having the same name as a class-level variable.  Within the scope of the method, the local variable will hide the class-level variable.

    public class MyClass
    {
        public int x = 10;

        public void MethodA()
        {
            double x = 4.2;
            Console.WriteLine(x);
        }
    }

1105-001
You cannot, however, reference a class-level variable before declaring the local variable, since this is interpreted as referencing the local variable before it is defined.

        public void MethodA()
        {
            // ERROR: Can't use local variable before it's declared
            Console.WriteLine(x);

            double x = 4.2;
            Console.WriteLine(x);
        }

You also can’t reference the class-level variable in an outer scope.

        public void MethodA()
        {
            Console.WriteLine(x);

            if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
            {
                // ERROR: Can't declare local variable within this scope
                double x = 4.2;
                Console.WriteLine(x);
            }
        }

#1,104 – Can’t Re-Declare a Variable within a Nested Block

A declaration space is a region of code within which you can’t declare two entities having the same name.  

Defining a class creates a new declaration space, i.e. you can declare variables at the class level.

Defining a method creates a new declaration space, known as a local variable declaration space.  A block of code within that method creates a nested local variable declaration space.

It’s of course an error to declare two variables of the same name within a local variable declaration space.  It’s also an error to re-declare a variable of the same name in a nested local variable declaration space.

        public void MethodA()
        {
            int y = 1;
            string y = "oops";  // ERROR

            int x = 12;
            Console.WriteLine(12);

            if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
            {
                // ERROR: Can't re-declare x within nested local variable declaration space
                double x = 4.2;
                Console.WriteLine(x);
            }
        }

#1,103 – A Block Defines Both Scope and Declaration Space

  • Scope is the region of code in which you can refer to a named entity using its unqualified name.
  • Declaration space is a region of code within which you can’t declare two entities having the same name

A block of statements defines both a new scope and a new declaration space.

In the code below, the variable msg is declared within the block of statements following the if statement.

    public class MyClass
    {
        public void AMethod()
        {
            if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
            {
                // Can define variables within this block
                string msg = "Hi";
                Console.WriteLine("{0}, it's Thursday. ", msg);
            }

            // ERROR: The name 'msg' does not exist in the current context
            Console.WriteLine(msg);
        }
    }

The block defines a new scope–the variable msg can be referred to by name anywhere within the block (after the declaration).  This block also defines a declaration space–you can only declare one variable named msg within this block.

#1,102 – Scope vs. Declaration Space

The terms scope and declaration space are similar, but slightly different.

  • Scope is the region of code in which you can refer to a named entity using its unqualified name.
  • Declaration space is a region of code within which you can’t declare two entities having the same name

For example:

    public class MyClass
    {
        int count1 = 1;
        int count2 = 2;

        public void AMethod()
        {
            int count2 = 22;

            Console.WriteLine(count1 + ", " + count2);
        }
    }

Then:

  • The class-level declarations of count1 and count2 are in the same scope and in the same declaration space
  • The body of function AMethod()
    • is included in the scope in which class-level fields are defined, i.e. you can reference them using their unqualified name.
    • defines a new declaration space, i.e. we can define a new variable of the same name as the class-level fields
    • defines a new scope, in which we can define variables, nested within outer scope

1102-001

#1,101 – Mathematical Constants

For your convenience, the System.Math class provides two useful mathematical constants, as fields.

  • Math.E – base of the natural logarithm, or Euler’s Number e, with value of approximately 2.71828
  • Math.PI – ratio of circle’s circumference to its diameter π, with value of approximately 3.14159

If you need to use either of these constants in your code, you can use the corresponding field in the Math class.

            double radius = 5.0;
            double circumference = 2.0 * Math.PI * radius;

            Console.Write("Radius={0}, Circumference={1}", radius, circumference);

1101-001