for Robot Artificial Inteligence

2.Logical-Operators

|

Example 1

#include <iostream>

using namespace std;

main()
{
    /*
        AND conjunction &&

        7 > 5 && 5 != 10 //conjunction is true ONLY if both expressions are true

        expr1 expr2 result
          0     0     0
          0     1     0
          1     0     0
          1     1     1

        OR disjunction || (alternative)

        disjunction is false ONLY when both expressions are false

        expr1 expr2 result
          0     0     0
          0     1     1
          1     0     1
          1     1     1

        ! (exclemation mark) - NOT

        ! (0) the result will be 1
        ! (1) the result will be 0
    */

    cout << !(5 > 7 || 5 != 10) << endl;


}

Example 2

#include <iostream>

using namespace std;

main() {

    int minValue, maxValue, elementToCheck;
    bool isContained;

    cout << "Please input the minimum value: ";
    cin >> minValue;
    cout << "Please input the maximum value: ";
    cin >> maxValue;
    cout << "Please input the number to check: ";
    cin >> elementToCheck;

    isContained = (minValue <= elementToCheck) && (elementToCheck <= maxValue);
    cout << "Is the number " << elementToCheck << " contained in the interval from " << minValue << " to " << maxValue << " ? " << isContained;

}

Example 3

#include <iostream>

using namespace std;

main() {

    int minValue, maxValue, elementToCheck;
    bool isGreaterThanMinValue, isLowerThanMaxValue;

    cout << "Please input the minimum value: ";
    cin >> minValue;
    cout << "Please input the maximum value: ";
    cin >> maxValue;
    cout << "Please input the number to check: ";
    cin >> elementToCheck;

    isGreaterThanMinValue = (minValue <= elementToCheck);
    isLowerThanMaxValue = (elementToCheck <= maxValue);

    cout << "Is the value " << elementToCheck << " greater or equal to " << minValue << " ? " << isGreaterThanMinValue << endl;
    cout << "Is the value " << elementToCheck << " lower or equal to " << maxValue << " ? " << isLowerThanMaxValue << endl;



}

Example 4

#include <iostream>

using namespace std;

main()
{
    /*
        0
        1
        0101 0110

        1 2 6 = 1 * 10 ^ 2 + 2 * 10 ^ 1 + 6 * 10 ^ 0 = 100 + 20 + 6

        1 0 1 0 = 2 ^ 3 + 2 ^ 1 = 8 + 2 = 10

        1 0 1 1 0 0 = 2 ^ 2 + 2 ^ 3 + 2 ^ 5 = 4 + 8 + 32 = 44
    */
    /*
        Bitwise AND - &
        Bitwise OR - |
        Bitwise NOT - ~ (tilde)
        Bitwise XOR - ^ (caret) eXclusive OR.
        0 0 0
        0 1 1
        1 0 1
        1 1 0
        Bitwise left shift <<
        Bitwise right shift >>
    */

    cout << (10 & 2) << endl;

    /*
        1 0 1 0
        0 0 1 0
        -------
        0 0 1 0
    */
    cout << (10 | 2) << endl;

    /*
        1 0 1 0
        0 0 1 0
        -------
        1 0 1 0
    */
    cout << (10 ^ 2) << endl;

    /*
        1 0 1 0
        0 0 1 0
        -------
        1 0 0 0
    */

    cout << (~10) << endl;

    /*
         000000000000000000000000000000000000 1 0 1 0
         111111111111111111111111111111111111 0 1 0 1
    */

    cout << (10 << 3) << endl; //this thing means that we are multiplying 10 by 2 raised to the power of 3
    /*
        000000000000000000000000000000000001 0 1 0 0
        20 - decimal notation
        40
    */

    cout << (10 >> 1) << endl; //this thing means that we are dividing 10 by 2 raised to the power of 3
    /*
        00000000000000000000000000000000000 0 1 0 1
        5
    */
}

Comments