From c50ff9299651ba3c856ee23cbdabeda2784b864c Mon Sep 17 00:00:00 2001 From: Jatin Dhankhar Date: Mon, 27 Jun 2016 19:00:07 +0530 Subject: Added the Logical and bitwise operators section, fixes merge issue with #1817 (#2292) * Added the Logical and bitwise operators section * Added a note for Short Circuit evaluation Excerpt from https://en.wikipedia.org/wiki/Short-circuit_evaluation C++ uses minimal evaluation, or McCarthy evaluation (after John McCarthy (computer scientist)) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true. In some programming languages (Lisp), the usual Boolean operators are short-circuit. --- c++.html.markdown | 57 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/c++.html.markdown b/c++.html.markdown index 6b81f95f..290633f3 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Geoff Liu", "http://geoffliu.me"] - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] + - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] lang: en --- @@ -1005,7 +1006,7 @@ cin>>val; Vector_name.push_back(val); // will push the value of variable into array // To iterate through vector, we have 2 choices -// using normal looping +// using normal looping for(int i=0; i // Will include the header file for sets @@ -1031,7 +1032,7 @@ ST.insert(20); // Will insert the value 20 in set ST ST.insert(30); // Will insert the value 30 in set ST // Now elements of sets are as follows // 10 20 30 - + // To erase an element ST.erase(20); // Will erase element with value 20 // Set ST: 10 30 @@ -1041,7 +1042,7 @@ for(it=ST.begin();it('A',1) ); mymap.insert ( pair('Z',26) ); // Will insert value 26 for key Z -// To iterate +// To iterate map::iterator it; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << "->" << it->second <<'\n'; -// Output: +// Output: // A->1 // Z->26 @@ -1078,6 +1079,50 @@ cout<second; // OUTPUT: 26 +/////////////////////////////////// +// Logical and Bitwise operators +////////////////////////////////// + +// Most of the operators in C++ are same as in other languages + +// Logical operators + +// C++ uses Short - circuit evaluation for boolean expressions, i.e, the second argument is executed or +// evaluated only if the first argument does not suffice to determine the value of the expression + +true && false // Performs **logical and** to yield false +true || false // Performs **logical or** to yield true +! true // Performs **logcical not** to yield + +// Instead of using symbols equivalent keywords can be used +true and false // Performs **logical and** to yield false +true or false // Performs **logical or** to yield true +not true // Performs **logcical not** to yield + +// Bitwise operators + +// **<<** Left Shift Operator +// << shifts bits to the left +4 << 1 // Shifts bits of 4 to left by 1 to give 8 +// x << n can be thought as x * 2^n + + +// **>>** Right Shift Operator +// << shifts bits to the right +4 >> 1 // Shifts bits of 4 to right by 1 to give 2 +// x << n can be thought as x / 2^n + +~4 // Performs a bitwise not +4 | 3 // Performs bitwise or +4 & 3 // Performs bitwise and +4 ^ 3 // Performs bitwise xor + +// Equivalent keywords are +compl 4 // Performs a bitwise not +4 bitor 3 // Performs bitwise or +4 bitand 3 // Performs bitwise and +4 xor 3 // Performs bitwise xor + ``` Further Reading: -- cgit v1.2.3