Arithmetic, Bitwise, Increment - Decrement and Equality - Comparison Operators in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 5077 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Null literal, Numeric types and Numeric literals in C# before this article.

Introduction

So far we have seen Numeric types in C# and here we are going to see some operators in C#.

Objective

The main objective of this article is to learn about operators in detail with examples that are used in C# programming.

Arithmetic Operators

In C#, arithmetic operators are defined for all the numeric types except 16-bit and 8-bit integral types. They are as follows,
  • +    Addition
  • -    Subtraction
  • *    Multiplication
  • /    Division
  • %   Percentage Division
These operators are used for Arithmetic operations such as Addition, Subtraction, Multiplication and Division.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static void Main()
        {
            int a = 10, b = 5;

            Console.WriteLine(a + b);  // 15 - Performs addition
            Console.WriteLine(a - b);  // 5 - Performs subtraction
            Console.WriteLine(a * b);  // 50 - Performs multiplication
            Console.WriteLine(a / b);  // 2 - Performs division
            Console.WriteLine(a % b);  // 0 - Shows reminder after division
        }  
    }
}
In the above code, we have performed all the arithmetic operations between a and b.

Note: Please read the comments in the code.

Output of the above code will be,


Increment and Decrement Operators

Increment operator increments the numeric value by 1. It is denoted as '++'.
Similarly, decrement operator decrements the numeric value by 1. It is denoted as '--'.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static void Main()
        {
            int a = 0, b = 0;
            Console.WriteLine(a++);  // Output 0 and a becomes 1
            Console.WriteLine(++b);  // Output 1

            Console.WriteLine(a++);  // Output is 1 and a becomes 2
            Console.WriteLine(++b);  // Output is 2 as it executes serially and incriments b

            Console.WriteLine(a--);  // Output is 2 and a becomes 1
            Console.WriteLine(--b);  // Output is 1 as decrements serially

            Console.WriteLine(a--);  // Output is 1 and a becomes 0
            Console.WriteLine(--b);  // Output is 0 decremented from 1
        }  
    }
}
In the above code, we have used increment and decrement operators several times to see their operation.

'a++' increments the value but prints the previous value where as, '++a' directly increments and prints the same.

Note: Please read the comments in the code.

Press Ctrl + F5 and see the output which is something like below,


Bitwise Operators

C# supports some bitwise operators as follows,
  • '~'    -  Complement
  • '&'    -  And
  • '|'    -  Or
  • '^'    -  Exclusive Or
  • '<<'  -  Shift left
  • '>>'  -  Shift right
Below code clearly explains about bitwise operators for decimal values in C#,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(~5);          // -6        : ~5(0101) means -6 (NOT a = - a - 1)
            Console.WriteLine(5 & 10);      // 0         : 5(0101) & 10(1010) = 0(0000)
            Console.WriteLine(5 | 10);      // 15        : 5(0101) | 10(1010) = 15(1111)
            Console.WriteLine(5 ^ 10);      // 15        : 5(0101) ^ 10(1010) = 15(1111)
            Console.WriteLine(5 << 1);      // 10        : 5(0101) << 1 digit gives 10(1010)
            Console.WriteLine(5 >> 1);      // 16        : 5(0101) >> 1 digit gives 2(0010)
        }
    }
}
Note: Please read the comments in the code.

Output of the above code will be,



Lets take an example code below for bitwise operations for Hexadecimal inputs,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(~0xfU);            // 4294967280 : which is  0xfffffff0  in Hexadecimal 
            Console.WriteLine(0xf0 & 0x33);      // 48         : which is  0x30        in Hexadecimal
            Console.WriteLine(0xf0 | 0x33);      // 243        : which is  0xf3        in Hexadecimal
            Console.WriteLine(0xff00 ^ 0x0ff0);  // 61680      : which is  0xf0f0      in Hexadecimal
            Console.WriteLine(0x20 << 2);        // 128        : which is  0x80        in Hexadecimal
            Console.WriteLine(0x20 >> 1);        // 16         : which is  0x10        in Hexadecimal
        }  
    }
}
In the above code, we have performed all the bitwise operators in C# for hexadecimal inputs. 

Note: Please read the comments in the code.

But the result comes in decimal form as follows,


Equality and Comparison Operators

Equality and comparison are almost same but the ways are different. Let's see the difference through below examples.
Equality operator:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static void Main()
        {
            int a = 10, 
                b = 5, 
                c = 8, 
                d = 10;

            Console.WriteLine(a == b);  // False  :  a and b are not same values
            Console.WriteLine(b == c);  // False  :  b and c are not same values
            Console.WriteLine(c == d);  // False  :  c and d are not same values
            Console.WriteLine(a == d);  // True   :  a and d are same values
        }  
    }
}
In the above code we are checking the equality between the integers we mentioned. 'a' and 'd' are equal values and so it will print 'true' only for that condition.

Note: Please read the comments in the code.

Output of the above code will be,


Comparison Operator:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        public class Student
        {
            public string Name;
            public Student(string n)
            {
                Name = n;
            }
        }
        static void Main()
        {
            Student s1 = new Student("Krishna");
            Student s2 = new Student("Krishna");
            Student s3 = s2;

            Console.WriteLine(s1 == s2);  //  False  :  Comparision operator works for all numeric types... 
            Console.WriteLine(s2 == s3);  //  True   :  Since both are same 
            Console.WriteLine(s1 != s2);  //  True   :  Because both are not equal dynamically
        }  
    }
}
In the above code, we have seen comparing strings directly which has given 'False'. Let's see output values of the above code below,


Equality and Comparison operators  such as ==, !=, <, >, <= and >= works for all the numeric types but should be careful with real numbers as we get rounding errors in real numbers. 

Conclusion

In this article, we have seen different operators used in C# with examples. Hope you understand them.

Thanks for reading.

Regards,
Krishna.



Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)