true false

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1044 Views - Last Post: 28 February 2012 - 09:02 AM Rate Topic: -----

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: true false

Posted 28 February 2012 - 05:24 AM

View PostCurtis Rutland, on 27 February 2012 - 02:39 PM, said:

View Postbaavgai, on 27 February 2012 - 11:54 AM, said:

So & and && are identical for boolean (in C#)!


Actually, that's not correct either, proven by the link you provided.


Quote

For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.
-- http://msdn.microsof...c(v=vs.80).aspx


Please explain how my statement is incorrect, when it's what Microsoft explicitly states. :P

This post has been edited by baavgai: 28 February 2012 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

#17 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: true false

Posted 28 February 2012 - 06:01 AM

This part:

Quote

C# has overloaded the & operator to behave like a && with bool types. So & and && are identical for boolean (in C#)!


Is incorrect, because it's not identical behavior. One uses short circuit logic, and the other doesn't. That's the point I was trying to get across. The result is the same, but the side effects can be different, which could be completely unimportant, or critically important, depending on the way you write the code.
Was This Post Helpful? 1
  • +
  • -

#18 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: true false

Posted 28 February 2012 - 07:24 AM

I think you're reading it wrong. And, I think I can prove it!

class Program {
	private void Write(string s) { Debug.Write(s); }

	private delegate bool BoolValue();
	private delegate bool Oper(BoolValue ba, BoolValue bb);

	private void ShowTruthTableRow(bool a, bool b , string name, Oper op) {
		BoolValue ba = delegate() { Write("[A called]"); return a; };
		BoolValue bb = delegate() { Write("[B called]"); return b ; };
		Write(a + " " + name + " " + B)/>;
		bool result = op(ba, bb);
		Write(" result = " + result + "\n");
	}

	private void ShowTruthTable(string name, Oper op) {
		ShowTruthTableRow(true, true, name, op);
		ShowTruthTableRow(true, false, name, op);
		ShowTruthTableRow(false, true, name, op);
		ShowTruthTableRow(false, false, name, op);
	}

	public void ShowTruthTable() {
		ShowTruthTable("&&", delegate(BoolValue ba, BoolValue bb) { return ba() && bb(); });
		ShowTruthTable("&", delegate(BoolValue ba, BoolValue bb) { return ba() & bb(); });
	}

	static void Main(string[] args) {
		new Program().ShowTruthTable();
	}
}



Results:
True && True[A called][B called] result = True
True && False[A called][B called] result = False
False && True[A called] result = False
False && False[A called] result = False
True & True[A called][B called] result = True
True & False[A called][B called] result = False
False & True[A called][B called] result = False
False & False[A called][B called] result = False



Right, well, um, I guess I'm reading it wrong. Thanks for the clarification. I had a fun time proving you right. :P
Was This Post Helpful? 2
  • +
  • -

#19 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: true false

Posted 28 February 2012 - 09:02 AM

Always fun to have a chance to experiment and learn something new. Especially when it's just for the hell of it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2