Boolean operators in C/C++?

Started by
6 comments, last by Drevay 21 years, 1 month ago
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-)(-
_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
Advertisement
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.
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)
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.)
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
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!!!

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?

Gah...sorry again for asking

Of course that''''s just my opinion, I could be wrong.
-)(-Dennis Miller-)(-
_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
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;}  

This topic is closed to new replies.

Advertisement