#### Archived

This topic is now archived and is closed to further replies.

# Boolean operators in C/C++?

This topic is 5614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I find them confusing at the moment, like the NOT, AND, OR operators, I mean, if is easy .... but..... ! = NOT || = OR && = AND but.....how do I use them properly? ?_? For instance - !(0 || 1) = ? !(0 || 0) = ? !(1 || 0) = ? !(1 || 0 && 0) = ? More examples are welcome, I mean, I ran into problems similar to this on cprogramming.com, and, they confused me. The author attempts to explain them, but it merely confuses me. Could someone explain how these are used and how to achieve these problem answers, in lamens terms? I ... really do feel like a noob, but, asking is the best way to learn :yes? Of course that''''s just my opinion, I could be wrong. -)(-Dennis Miller-)(-

##### Share on other sites
quote:
Original post by Drevay
For instance -

!(0 || 1) = ?
!(0 || 0) = ?
!(1 || 0) = ?
!(1 || 0 && 0) = ?

Ah, see, part of our problem is that you''re attempting to apply Boolean operators to non-Boolean values. 0 and 1 are integer; apply the bitwise equivalents to those: ! (the same), | and &.
!(false || true) = !(true) = false!(false || false) = !(false) = true!(true || false) = !(false || true) = false [OR is commutative]!(true || false && false) = !(true || false) = false [AND has higher precedence]

When using these kinds of expressions, you can replace my explicit trues and falses with expressions/statements that evaluate to boolean values.

##### Share on other sites
The bitwise equivalent to logical NOT ( ! ) is ~

Oluseyi, I might come off a bit Sabreman when I say this, but if you don''t know the answer, don''t reply.

Drevay:

if(true && false) // always false

if(false && false) // always true

if(true && true) // always true

if(!false) // true

if(!true) // false

unsigned char a = 0x00;
unsigned char b = 0xff;

if(~b == a) // true
if(b & a) // false
if(b | a) // true
if(~b) // false
if(a) // false
if(!a) // !false -> true
if(~a) // ~0x00 = 0xff; 0xff != 0 -> true

~, ^, |, &, <<, and >> are to be used on integers only. Their results are also integers.

!, ||, and && are to be used on boolean values. There is no logical version of ^ (exclusive or) but that''s ok since

a xor b == (a || b) && !(a && b)

##### Share on other sites
quote:
Original post by flangazor
The bitwise equivalent to logical NOT ( ! ) is ~

Correct. Somehow I had forgotten that.

quote:
Oluseyi, I might come off a bit Sabreman when I say this, but if you don''t know the answer, don''t reply.

Sheesh! No one can make an honest mistake anymore.

quote:
if(false && false) // always true

Always false. Anything AND-ed with false is false. Go check your truth tables. (Honest mistake, I know.)

##### Share on other sites
I always find it useful only to remember what evaluates to true, so that everything else is just false. For example, with AND, just remember that only two 1 bits is true. That''s all you have to remember, and you almost imply to yourself that everything else if false. I remeber that XOR evalutes to true if the bits are different. I also remember that if there''s a 1 somewhere, OR evalutes to true.

Just depends how you remember it. I found my system to be quite effective, but then again it''s my system

##### Share on other sites
You got me! I somehow replaced AND with NOT XOR in my brain.

I knew I would make a mistake if I heckled you for it.

Darn you Karma. Darn youuuuuuuuuu!!!

##### Share on other sites
Ok, I''m sorry for asking >:

But anyway, I am just learning man, I really want to learn, it''s not like I didn''t even try to learn.... I just thought you guys would help me

But, you did, hmmm..... I''m still a tad confused though.....

When are these used in a program..........like.........an example?

Of course that''''s just my opinion, I could be wrong.
-)(-Dennis Miller-)(-

##### Share on other sites
An example:

  #include <iostream>#include <string>int main(){  std::string word;  std::cout << "I want a cookie" << std::endl;    std::cin >> word;  while(word != "cookie") // <== EXAMPLE 1  {    std::cin >> word;  }  std::cout << "Now I want some milk." << std::endl;  while(word != "milk" || word != "JennaJameson") // <== EXAMPLE 2  {    std::cin >> word;  }  std::cout << "That was delicious." << std::endl;  return 0;}

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631712
• Total Posts
3001849
×