C++ Alternative Operator Representations

In this tutorial, we are going to learn about the alternative operator representations in C++ programming language with examples.
Submitted by IncludeHelp, on May 16, 2020

There are the following alternative keywords to the various operators (mentioned below), all these keywords operate similarly the same as its primary tokens. For example: and behaves the same as &, and_eq behaves the same &=, etc.

The keywords are,

PrimaryAlternative keywords/tokens
&&and
&=and_eq
&bitand
|bitor
~compl
!not
!=not_eq
||or
|=or_eq
^xor
^=xor_eq
{<%
}%>
[<:
]:>
#%:
##%:%

C++ Example for Alternative Operator Representations

/*
C++ example to demonstrate the use of
alternative operator keywords
*/

#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    int a = 10;
    bitset<4> value("1111");
    bitset<4> mask1("1010");

    // and, or, not, not_eq keywords
    cout << "a: " << a << endl;
    cout << "(a>5 and a<20): " << (a > 5 and a < 20) << endl;
    cout << "(a>5 or a<20): " << (a > 5 or a < 20) << endl;
    cout << "not(a>5 and a<20): " << not(a > 5 and a < 20) << endl;
    cout << "(a not_eq 5): " << (a not_eq 5) << endl;

    //bitand, bitor, compl, and_eq, or_eq
    //xor, xor_eq keywords
    cout << "value: " << value << endl;
    cout << "mask1: " << mask1 << endl;
    cout << "(value bitand mask1): " << (value bitand mask1) << endl;
    cout << "(value bitor mask1): " << (value bitor mask1) << endl;
    cout << "(value xor mask1): " << (value xor mask1) << endl;
    cout << "compl value: " << compl value << endl;
    value and_eq mask1;
    cout << "value and_eq mask1: " << value << endl;
    value or_eq mask1;
    cout << "value or_eq mask1: " << value << endl;
    value xor_eq mask1;
    cout << "value xor_eq mask1: " << value << endl;
 
    return 0;
}

Output

a: 10
(a>5 and a<20): 1
(a>5 or a<20): 1
not(a>5 and a<20): 0
(a not_eq 5): 1
value: 1111
mask1: 1010
(value bitand mask1): 1010
(value bitor mask1): 1111
(value xor mask1): 0101
compl value: 0000
value and_eq mask1: 1010
value or_eq mask1: 1010
value xor_eq mask1: 0000

Reference: C++ operator alternative







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.