mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
* 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.
This commit is contained in:
parent
46844f8e8e
commit
c50ff92996
@ -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
|
||||
---
|
||||
|
||||
@ -1078,6 +1079,50 @@ cout<<it->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:
|
||||
|
Loading…
Reference in New Issue
Block a user