-3

Simply put, is there and operator for the AND OR concept?

EX: if (a = true AND OR b = true)

If not an operator, what process could be done to do this?

Laina
  • 29
  • 1
  • 7
  • C++ isn't English. Can you explain with actual C++ code? – PaulMcKenzie Sep 04 '14 at 04:30
  • In C++ (as in most programming language), the logical `or` is an inclusive or, so the result is true if its left operand is true, or its right operand is true, or if both are true. – Jerry Coffin Sep 04 '14 at 04:32
  • @PaulMcKenzie if (a = true && || b = true) Yes I know this is a bad way to write it, but it's there to ask the question. The question if an action like that is possible. – Laina Sep 04 '14 at 04:33
  • `&& ||` together not work. what you want `&&` or `||`? You can use like `if(condition1 && condition2)` or `if(condition1 || condition2)` – Jayesh Bhoi Sep 04 '14 at 04:33
  • @Jayesh I want both. That's why I wrote the question the way I did. "You can have a AND OR b." – Laina Sep 04 '14 at 04:35
  • @Laina So use `||`. if both condition true then also it will work and if one of condition true then also it work. – Jayesh Bhoi Sep 04 '14 at 04:39
  • @Laina - The problem with using English as a tool in figuring out C++'s logical operations is that invariably a person can fall into this trap: `if (x == 10 || 11 || 12)`. This *does not* do what you think it does, even though this is how we speak in English (if x is equal to 10 or 11 or 12). – PaulMcKenzie Sep 04 '14 at 04:40
  • @Paul: even worse: `if (x == 10, 11 || 12) ...` which is how I would say it :-) – paxdiablo Sep 04 '14 at 04:46
  • @paxdiablo - I've seen worse than that, where the newbie programmer literally translates how we speak in English to C++ using and's and or's. And of course, the code compiled with no errors, all to produce disastrous results when run. – PaulMcKenzie Sep 04 '14 at 04:48

4 Answers4

4

The English concept and/or is already represented in C-based languages as ||, which is inclusive.

By that, I mean a || b is true if a is true, or b is true or both are true.

The "you are a boy or a girl" meaning of "or" (either "you are a boy" or "you are a girl", but not both) is exclusive, and is not really catered for with primitive operators.

To achieve that, you could use an expression like:

a != b

assuming they were values generated by a Boolean operation, rather than arbitrary integers (which C will also allow).

For treating arbitrary integers the same way C does, use:

!!a != !!b

The double Boolean negation will force them to be "proper" Boolean values before comparison.


If you want a more in-depth examination of the different bitwise operators, see this answer, or refer to the following tables, where F indicates false (or zero) and T represents true (or one):

AND | F T     OR | F T     XOR | F T    NOT | F T
----+-----    ---+----     ----+----    ----+----
 F  | F F      F | F T       F | F T        | T F
 T  | F T      T | T T       T | T F
Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thank you! It's a noob question I know, and haven't been able find my answere in google because the way you explained it is different from what I've been reading. So thank you for bearing with me. I do appreciate it. – Laina Sep 04 '14 at 04:41
  • No problems, @Laina, SO is meant for _all_ levels of programmer, and Google is notoriously unsuited for finding terms with lots of punctuation. Things like Perl (which seem to be _all_ punctuation) are an absolute nightmare :-) – paxdiablo Sep 04 '14 at 04:44
0

In most programming languages, including C and C++, logical OR (||) takes its meaning from mathematics, and is therefore inclusive: either A or B is true, or both are true.

  • true OR true = true
  • true OR false = true
  • false OR true = true
  • false OR false = false

In English, we say “and/or” because “or” is typically exclusive, akin to logical XOR: either A or B is true, but not both.

  • true XOR true = false
  • true XOR false = true
  • false XOR true = true
  • false XOR false = false

Most languages lack a logical XOR operator, because it’s equivalent to not-equals (!=) for Booleans.

bool xor(bool a, bool b) {
  return a != b;
}
Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
0

Did you mean XOR (Exclusive OR)?

The short answer for what the XOR operator is: ^

But from the looks of it, I think you may need a stronger foundation of how variables are compared, you may want to get familiar with this: http://www.cplusplus.com/doc/tutorial/operators/

gyeo
  • 63
  • 6
0

Saying if A is true AND OR B is true isn't a correct programming expression nor is it a correct expression in Boolean algebra. You cant have a conjuncture and disjuncture like this in a statement. You cant represent that statement in a Venn diagram or a logic gate, and you should be able to do both with any valid Boolean expression. To see this try and write your question in a truth table.

  • This actually does explain quite a bit. I've probably been going at my programs with a wrong mind set. I will start going at my work from a truth table approach if not actually write them out as such before programming. Tedious but it will help. Thank you. – Laina Sep 04 '14 at 04:54