德尔福:对于布尔值,XOR vs <>

Pet*_*ner 2 delphi inequality boolean xor

之间有什么区别:

procedure InequalityMsg(ABool1, ABool2 : Boolean);
begin
  if ABool1 <> ABool2 then
    ShowMessage('Yeah, they''re not the same');
end;
Run Code Online (Sandbox Code Playgroud)

procedure InequalityMsg(ABool1, ABool2 : Boolean);
begin
  if ABool1 XOR ABool2 then
    ShowMessage('Yeah, they''re not the same');
end;
Run Code Online (Sandbox Code Playgroud)

And*_*and 8

不,他们完全一样.(好吧,生成的代码可能会有所不同,但行为永远不会显示出任何差异.而且,随着性能的提高,这个问题非常不重要.)

  • (假和假)=假,(假=假)=真 (9认同)
  • 如果你的`Boolean`变量通过输入一个没有值为0或1的整数来获取它的值,可能会有细微的差别.(当你输入类型时,你告诉编译器你知道你是什么正在做,它信任你.)你可能最终得到两个非零的非零变量.如果你是类型转换整数,你应该使用`LongBool`,`WordBool`或`ByteBool`而不是`Boolean`.Delphi假设`Boolean`只有两个可能的二进制值; 它没有对其他类型做出相同的假设. (7认同)
  • AND和=不给出相同的结果. (4认同)