Logical operations (C#)
Blog > C# > Logical operations (C#)

Logical operations (C#)

PL

Logical operations C#

During programming, the necessity of logically checking the values of variables constantly appears. Logical operations have a wide variety of applications, for example, for controlling occurring events, checking the availability of functions, or managing logged in people on the site.


Contents


Programming language C# provides the logical operations listed below:

AND (logical product)x & y
XOR (sum modulo 2)x ^ y
OR (logical sum)x | y
NOT (negation)!
Conditional ANDx && y
Conditional ORx || y
Logical operations C#
Logical operations C#

Operator & – AND


A unary operator that performs a logical product operation. It checks the two given values and returns true only if both are also true.


if(false & false)
{
//there is a code that will not be executed
}

if(true & false)
{
//this code will also not be executed
}

if(true & true)
{
//this code will be executed
}

The & operator performs both arguments when executing the comparison, so that the code given below calls both the function WykonajFunkcje1() and WykonajFunkcje2().


if(WykonajFunkcje1() & WykonajFunkcje2())
{
//there is a code that will not be executed
}

The operator can also be used to perform a logical sum on numbers. Performing such an operation is presented in the example below:


// Operation of AND logical sum of hexadecimal numbers
// A9 (1010 1001) and 3D (0011 1101).
// 1010 1001
// 0011 1101
// ---------
// 0010 1001 czyli 29
Console.WriteLine("0x{0:x}", 0xa9 & 0x3D);
//the {0:x} entry displays the number in hexadecimal

Operator ^ – XOR


The operator performs the modulo 2 sum operation and returns true only if it checks two different values: one being true and the other false. If two true or two values, false returns false.


if(false ^ false)
{
//there is a code that will not be executed
}

if(true ^ true)
{
//this code will also not be executed
}

if(true ^ false)
{
//this code will be executed
}

The XOR operation can also be performed on numbers. In this case, the result is calculated by the sum of modulo 2 from each pair of bits of given numbers. The calculation method is presented below:


// XOR operation of hexadecimal numbers
// A9 (1010 1001) and 3D (0011 1101).
// 1010 1001
// 0011 1101
// ---------
// 1001 0100 czyli 94
Console.WriteLine("0x{0:x}", 0xa9 ^ 0x3D);
//the {0:x} entry displays the number in hexadecimal

Operator | – OR


The operator performs the operation of a logical sum, which means that it is enough for one of the elements to have the value true so that the result of the operation would also be true. Both values are checked, so as with the operator & when they are functions, both will be called.


if(false | false)
{
//there is a code that will not be executed
}

if(true | false)
{
//this code will be executed
}

if(true | true)
{
//this code will also be executed
}

The operator realizes the logical sum also on bits of numbers given as values:

// XOR operation of hexadecimal numbers
// A9 (1010 1001) and 3D (0011 1101).
// 1010 1001
// 0011 1101
// ---------
// 1011 1101 czyli BD
Console.WriteLine("0x{0:x}", 0xa9 | 0x3D);
//the {0:x} entry displays the number in hexadecimal

Operator ! – NOT


The operator is defined for the bool type and realizes negations. If true, return false and vice versa.


Console.WriteLine(!true); //Displays the value false
Console.WriteLine(!false); //Displays the value true

Operator && – conditional AND – Logical operations


The conditional operator AND performs the same operation as the operator & adding some assumption to it. The logical product is only true if both elements are true, so it is enough for one of them to be false and the whole expression will be false. This dependency uses conditional AND. When checking, when the first element is false, the second element is not checked and false is returned. The implementation of a logical product in such a way can significantly accelerate the performance of operations. Consider this in the following example:


// One of the values that will be compared is false
bool warunek1 = false;
bool warunek2 = true, warunek3 = true, warunek4 = true, warunek5 = true, warunek6 = true;

// The following example checks warunek1, następnie warunek2, warunek3, and so on.
if(warunek1 & warunek2 & warunek3 & warunek4 & warunek5 & warunek6)
Console.WriteLine("0x{0:x}", 0xa9 | 0x3d);

// The following example checks the condition1, states that the operation
// it can not be made and ends at this stage
if(warunek1 && warunek2 && warunek3 && warunek4 && warunek5 && warunek6)
Console.WriteLine("0x{0:x}", 0xa9 | 0x3d);

Knowing the difference between & && operators is important when the values given to the product are functions. In the case of the && operator, you can never assume that all functions will be started, because the operation will end at the first false value encountered.

Operator || – conditional OR

As in the operator described above, some property of a logical sum is used here. It’s enough that one of the elements will be true to make the whole expression also true. Therefore, the first true encountered stops the checking of other conditions and runs the code provided.


As in the && operator, you can not assume that all checked functions will be run because after the first one that returns true check will end.


// Variables with different values
bool warunek1 = true, warunek2 = false, warunek3 = true, warunek4 = true, warunek5 = false, warunek6 = false;

// The following example checks the następnie warunek2, warunek3, and so on
if(warunek1 | warunek2 | warunek3 | warunek4 | warunek5 | warunek6)
Console.WriteLine("0x{0:x}", 0xa9 | 0x3d);

// The following example checks the condition1, states that the operation
// can be made and executes the code below
if(warunek1 || warunek2 || warunek3 || warunek4 || warunek5 || warunek6)
Console.WriteLine("0x{0:x}", 0xa9 | 0x3d);

The order of performing logical operations


The sequence of operations is strictly defined and presented below from the operations performed at the earliest:


  1. Negation
  2. XOR
  3. Logical product
  4. Logical sum
×
Any questions?
TOP