6.Ternary operator and 7. Bitwise Operators

 6.Ternary operator:

              It is a conditional operator(?:) which minimalize the conditional statements.it is easily inserted between code.

Syntax:

Symbol: ‘?:’

(condition)? statement: else statement;

(x > y)? x: y;

C++ program: which is greater one?

#include <iostream>

using namespace std;

int main() {

    int a, b;

    cout<<"Enter the two numbers" <<endl;

    cin>> a >> b;

        // Using ternary operator

    int g_val = (a > b) ? a : b;

    cout << "The biggest of two numbers is: " << g_val;

    return 0;

}

Output:

Enter the two numbers

45 56

The biggest of two numbers is: 56

7. Bitwise Operators:

              If you want to perform bitwise operations on integers, these operators are used. Here, is the list of bitwise operators.

  • ‘&’ Bitwise AND
  • ‘|’ Bitwise OR
  • ‘^’ Bitwise XOR
  • ‘~’ Bitwise NOT
  • ‘<<’ Left Shift
  • ‘>>’ Right Shift

C++ Program:

#include <iostream>

using namespace std;

int main() {

int x = 9; //  1001

int y = 5; //  0101

// Bitwise NOT example

int bw_not = ~x;

// Bitwise AND sample

int bw_and = x & y;

// Bitwise OR code

int bw_or = x | y;

// Bitwise XOR sample

 int bw_xor = x ^ y;

 // Bitwise Left Shift example

  int l_shift = x << 2;

 // Bitwise Right Shift example

  int r_shift = y >> 2;

// Display  the Results of  Bitwise Operators

    cout << "AND Operator: " << bw_and << endl;

    cout << "OR Operator: " << bw_or << endl;

    cout << "XOR Operator: " << bw_xor << endl;

    cout << "NOT Operator: " << bw_not << endl;

    cout << "Left Shift Operator: " << l_shift << endl;

    cout << "Right Shift Operator: " << r_shift << endl;

    return 0;

}

Output:

AND Operator: 1

OR Operator: 13

XOR Operator: 12

NOT Operator: -10

Left Shift Operator: 36

Right Shift Operator: 1

That’s all. The Unary operators in c++ were explained. Hope, this code is useful to you. Keep coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.