From: Dan Doel Date: 2003-08-11T10:01:50+09:00 Subject: Re: Ruby and OOP-design (question of an old "procedural person" ;) Mark J. Reed wrote: >On Mon, Aug 11, 2003 at 08:44:23AM +0900, Dan Doel wrote: > > >>I'm not sure about C, but I would assume it is, because there's no >>booleans in C so it'd be totally useless >>as it would do the exact same thing as &. >> >> > >No, it wouldn't. C doesn't have a Boolean *type*, but it does >have the idea of truth and falsehood (needed for if statements, etc). >Nonzero is true and zero is false. Which means that this expression: > > 1 & 2 > >is false - bitwise AND of 1 and 2 yields 0. Whereas > > 1 && 2 > >is true, because both 1 and 2 are true (nonzero). The big difference in C >is that the double-characters are LOGICAL operators, while the single >ones are BIT operators. > >However, it happens that the logical operators in C and derivatives >also short-circuit, which means you can do things like > > if (p != NULL && strcmp(p->value, key)==0) > { > >and not worry about the program blowing up because the strcmp() is called >when p is NULL. > > Yes, very true. I think I need to get more sleep or something. | and || would work equivalently, since you can't get a | b == 0 unless a == 0 and b == 0. & and && would be different, you're right. And, yes, logical && and || are lazy in C which answers the original question. I'm not sure why C doesn't have ^^ for logical xor, since 1 ^ 2 would be 3, which doesn't work for boolean logic. In Java, for example, though, it doesn't make a difference, because only booleans can be used as booleans, so there's no reason to have separate logical and bitwise operators, other than clarity of intent. Perhaps xor is just so seldom used that people don't care. - Dan