rcpp4everyone_en
rcpp4everyone_en copied to clipboard
Logical Operator Clarification
Thanks for this awesome book. It's been my go to reference for all things Rcpp. One thing that I think that could be clarified though is the section on logical operators (chapter 11). Coming from an R background, I expected &
and |
to take and return logical values, but my code wasn't working. After a bit of digging, I learned it was because &&
and ||
are the logical operators in C++ while &
and |
are bitwise (1 is interpreted as TRUE and everything else is interpreted as FALSE).
Most people from an R background would probably be looking for the logical operators so a clarification and a table like this could be useful.
Operator | Symbol | Form | Operation |
---|---|---|---|
Logical NOT | ! |
!x |
true if x is false, or false if x is true |
Logical AND | && |
x && y |
true if both x and y are true, false otherwise |
Logical OR | || |
x || y |
true if either x or y are true, false otherwise |
Logical Operators in C++ (Source: learnCpp.com)
Thanks, @ailich. Yea, that may be a confusing point. I would consider adding the explanation to the Chapter on the LogicalVector.
I really appreciate this kind of advice to make the book sensible for people who are not familiar with C++.