C# Tutorial Part 4.1 – Operators Continued

Welcome back to my C# operators lesson. In this installment of the tutorial, we will continue to work through the list of operators from the previous lesson, covering what each operator does and how it is used.

Logical Negation Operator!

This operator takes true/false values and flips them over, meaning anything that is true becomes false, and vice versa.
It’s symbol is the exclamation mark (!)
It is useful for selection and iteration (If statements and loops), which we will cover in the coming lessons, but can also be used on boolean variables, which we have already covered. Here is a demonstration of how it could be used:

using System;
class ProgramStartClass
    static void Main()

happy = false; //Declares a boolean variable called ‘happy’ and initialises it as false
        Console.WriteLine(“Am I happy?”); //Prints a question
        Console.WriteLine(happy); //Prints the value of happy (false)
        Console.WriteLine(“Now am I happy?”); //Asks the question again
        Console.WriteLine(!happy); //Prints the value of happy with the logical negation operator applied
        Console.ReadLine(); //Gives the user time to read the output by forcing the computer to wait for an input

Normally in these tutorials I write short pieces of code, but that was a fully functioning program, which could be pasted into a blank console application and would work. I wrote it out in full to remind you how to start a program, usually with “using System;”, then a class (with whatever name you wish) and a static void Main method.
The program functions as described in the comments, with the value of our boolean variable, happy, being outputted twice, once without the logical negation operator and once with it.
As you can see, the logical negation operator is used by putting it in front of a true/false expression, such as a boolean variable. It will alter the way the expression is read, so instead of the true value of the variable happy being printed, the opposite is printed.
The value of happy has not changed, the logical negation operator simply altered the way it was read. If there was to be a third printing of the variable, without the logical negation operator, the output would be “False”
It can, however be used to reverse the value of a boolean variable, as in this example:

bool hungry = true; //Declares a boolean variable called ‘hungry’ and initialises it as false
Console.WriteLine(hungry); //Prints the value of hungry
hungry = !hungry; //Flips the value of hungry
Console.WriteLine(hungry); //Prints the value of hungry

The variables in this code snippet tell you what is going on (although you shouldn’t need them), so we can focus on the third line:
hungry = !hungry;
This uses the assignment operator to give the variable ‘hungry’ a new value.
! can be taken to mean “the opposite of”, so what this line is saying is “hungry becomes the opposite of hungry”, or “hungry’s value is flipped”.
That line can be used to toggle the value of a boolean variable (it doesn’t always have to be called hungry) because it doesn’t matter whether it was true or false originally, it will always be the opposite at the end.

Bitwise Negation Operator ~

This operator flips the binary digits of a number, with binary 1s becoming binary 0s
It is used in exactly the same way as the logical negation operator.
Note that with signed data types it turns positive numbers into slightly lower than their negative counterpart. (It turns 10 into -11, for example). This should not be used as a way of making numbers negative – to do that just multiply by -1. There won’t really be many times you’ll have to use it at all, actually.
With unsigned data types it turns very high values into very low values and vice versa. 10 becomes 4294967285.

Increment and Decrement

To increment is to increase by 1 and to decrement is to decrease by 1.
If you wanted to increase something by 1 without using the increment operator, you would have to write:

day = day + 1; //Assuming you have a variable called ‘day’ which is of a numerical type


day += 1; //Uses the quick operation technique from the last lesson

But it is much quicker just to write:

day++ //Uses the increment operator

Or to decrease by one:

day– //Two ‘-‘ signs without a space between them

(You don’t have to type the comments, obviously.)
It may seem stupid to have so many ways of increasing by one, but when you come to be creating loops, you’ll be glad the increment operator makes it so quick.

A more complicated thing to do with incrementation is post-incrementation and pre-incrementation. Here’s a demo:

int A = 5, B = 2; //You know what this is doing
A = ++B; //Adds 1 to B and copies its value to A.

This is pre-incrementation, so called because the incrementation happens before calculations/assignment takes place.

A = B++; //Copies the value of B to A, then adds 1 to B. B will be 1 greater than A

This is post-incrementation – The incrementation happens after the assignment. It performs two different tasks on the same line – it’s just a time saver.

Of course, this also works for decrementation:

A = –B; //Subtracts 1 from B, then copies it’s new value to A. Note that it is two ‘-‘ signs with no space between them.

A = B–; //Copies the value of B to A then subtracts 1 from B

That’s all for this lesson. Expect questions to test your understanding in Part 4.2, then If statements in Part 5.0!


About Matt

I like writing, filmmaking, programming and gaming, and prefer creating media to consuming it. On the topic of consumption, I'm also a big fan of eating.
This entry was posted in Programming and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Enter comment:

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s