English 中文(简体)
为什么没有合乎逻辑的XOR?
原标题:Why is there no logical XOR?

为什么在 Java里没有逻辑的<代码>XOR?

问题回答

JavaScript traces its ancestry back to C, and C does not have a logical XOR operator. Mainly because it s not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

如果你有两个ean变变量,那么你可以将XOR与:

if (a != b)

有了两种任意的变量,你可以使用<代码>!,强迫他们 b美价值,然后使用相同的trick:

if (!a != !b)

虽然如此模糊,肯定值得一提。 确实,你甚至可以在此刻使用双向的XOR运营商,尽管这对我来说太cl了:

if (!a ^ !b)

Javascript has a bitwise XOR营运人:^

var nb = 5^9 // = 12

你们可以使用机器人,其结果将达到0或1(你可以改用手法,例如result = !(op1 ^ op2))。 但是,正如John所说,它相当于<代码>result = (op1 != op2),这一点更为清楚。

两名青少年的XOR完全是不是不同的。

Boolean(a) !== Boolean(b)

Java文中没有真正的精干操作者(但!相当接近)。 符合逻辑的操作员只能将truefalse作为操作程序,并且只能将truefal

Java字母&&>> > 采用各种歌剧,并交还各种不愉快的结果(无论你向这些剧团输入)。

合乎逻辑的操作者也应当始终考虑到两种操作方法的价值。

In Javascript && and : > > /code> take azy sh或tcut and do not Assessing the second conductnd in certain cases and出其副作用. 这种行为不可能与一个合乎逻辑的x或重新发生。


a() && b() evaluates a() and returns the result if it s falsy. Otherwise it evaluates b() and returns the result. Theref或e the returned result is truthy if both results are truthy, and falsy otherwise.

a() || b() evaluates a() and returns the result if it s truthy. Otherwise it evaluates b() and returns the result. Theref或e the returned result is falsy if both results are falsy, and truthy otherwise.

因此,总的想法是首先评估左轮操作。 只有在必要的情况下,才对正确操作进行评价。 其最后价值是结果。 这一结果可能毫无结果。 页: 1

这使得能够撰写像样的东西。

image = image || new Image(); // default to a new Image

src = image && image.src; // only read out src if we have an image

But the truth value of this result can also be used to decide if a "real" logical operat或 would have returned true 或 false.

这使得能够撰写像样的东西。

if (typeof image.hasAttribute ===  function  && image.hasAttribute( src )) {

if (image.hasAttribute( alt ) || image.hasAttribute( title )) {

But a "logical" x或 operat或 (^^) would always have to evaluate both operands. This makes it different to the other "logical" operat或s which evaluate the second operand only if necessary. I think this is why there is no "logical" x或 in Javascript, to avoid confusion.


So what should happen if both operands are falsy? Both could be returned. But only one can be returned. Which one? The first one? Or the second one? My intuition tells me to return the first but usually "logical" operat或s evaluate from left to right and return the last evaluated value. Or maybe an array containing both values?

And if one operand is truthy and the other operand is falsy, an x或 should return the truthy one. Or maybe an array containing the truthy one, to make it compatible with the previous case?

And finally, what should happen if both operands are truthy? You would expect something falsy. But there are no falsy results. So the operation shouldn t return anything. So maybe undefined 或 .. an empty array? But an empty array is still truthy.

采用阵列方法后,您将最后采用如下条件:if(a ^ b)。 (1){。 混淆不清。

a. 转向养羊,然后做 like等;

!!a ^ !!b

......

if( foo ? !bar : bar ) {
  ...
}

或更容易读:

if( ( foo && !bar ) || ( !foo && bar ) ) {
  ...
}

为什么?

由于javascript developers认为没有必要,因为其他已经执行、合乎逻辑的运营商可以表明这一点。

你同样可以 n笑,而且你可以 impress笑,你能够从中看到其他可能的逻辑行动。

i 本人认为,它有历史原因,从基于 c的语种向我所知的Xor不在场,或至少是不寻常的。

Yes, Just do the following. Assuming that you are dealing with booleans A and B, then A XOR B value can be calculated in JavaScript using the following

var xor1 = a !== b;

前一行也相当于以下各点:

var xor2 = (!a !== !b);

我本人更喜欢<代码>xor1。 既然我只字不提。 我认为,<代码>xor1也更快。 它只是进行两项计算:xor2

视觉解释...... 阅读表 代表<代码>fal和1 页: 1

!(A ==B):

| A | B | A XOR B | A === B | !(A === B) |
------------------------------------------
| 0 | 0 |    0    |    1    |      0     |
| 0 | 1 |    1    |    0    |      1     |
| 1 | 0 |    1    |    0    |      1     |
| 1 | 1 |    0    |    1    |      0     |
------------------------------------------

欢乐。

检查:

你可以这样说:

if( ( foo && !bar ) || ( !foo && bar ) ) {
  ...
}

如何以双重否定的方式将结果int改为bool? 不是这样,而是真的契约。

var state1 = false,
    state2 = true;
    
var A = state1 ^ state2;     // will become 1
var B = !!(state1 ^ state2); // will become true
console.log(A);
console.log(B);

为了邮局,而且由于我认为这是一件好事,你可以非常容易地利用真理,使XOR的运营商不得不进行胁迫。 如同所选择的答案一样,它很可能是太紧的。

const xor = (a, b) => !!(!!a ^ !!b)

console.log(undefined ^ {}) // Returns 0, bitwise can t be done here.
console.log(xor(undefined, {})) // Returns true, because {} is truthy and undefined is falsy
console.log(0 ^ 1) // Works naturally, returns 1
console.log(xor(0, 1)) // Also works, returns true
console.log(true ^ false) // Again, returns true
console.log(xor(true, false)) // And again, returns true...

And for fun, this should work in TypeScript, by forcing explicit any:

const xor = (a: any, b: any) => !!((!!a as any) ^ (!!b as any))

Boolean的一个线人:

if (x ? !y : y) { do something cool }

在上述xor功能中,它将产生以下结果:SIMILAR<>/strong> ,因为合乎逻辑的xor,意思是:“false for Equal Value”true for different Value",其中附有相应的数据。

This xor function will work as actual xor or logical operator, means it will result true or false according to the passing values are truthy or falsy. Use according to your needs

function xor(x,y){return true==(!!x!==!!y);}

function xnor(x,y){return !xor(x,y);}

之所以没有逻辑的XOR(^)是因为不同于和;&以及 Kluwer,没有给任何酶-生态优势。 这是关于这项权利的两种表述,必须加以评估。

<>p> (数字数值+变化):

value : number = (+false ^ +true)

因此:

value : boolean = (+false ^ +true) == 1

cond1 xor cond2相当于cond1 + cond 2 = 1:

兹证明:

let ops = [[false, false],[false, true], [true, false], [true, true]];

function xor(cond1, cond2){
  return cond1 + cond2 == 1;
}

for(op of ops){
  console.log(`${op[0]} xor ${op[1]} is ${xor(op[0], op[1])}`)
}

www.un.org/Depts/DGACM/index_spanish.htm 这里的另一种解决办法是使用2+变量,提供奖金。

这里有一个更一般性的解决办法,即模拟任何真相/外来价值观的逻辑性XOR,就像你在《国际自由论坛》标准声明中拥有经营者一样:

const v1 = true;
const v2 = -1; // truthy (warning, as always)
const v3 = ""; // falsy
const v4 = 783; // truthy
const v5 = false;

if( ( !!v1 + !!v2 + !!v3 + !!v4 + !!v5 ) === 1 )
  document.write( `[ ${v1} XOR ${v2} XOR "${v3}" XOR ${v4} XOR ${v5} ] is TRUE!` );
else
  document.write( `[ ${v1} XOR ${v2} XOR "${v3}" XOR ${v4} XOR ${v5} ] is FALSE!` );

我之所以这样说,是因为它也回答了“这些变量中有许多是真实的?”,因此,我通常会预先提出这一结果。

对于那些想要严格控制细菌-生物-生物-生物-生物-动物检查行为的人来说,情况就是这样:

if( ( ( v1===true ) + ( v2===true ) + ( v3===true ) + ( v4===true ) + ( v5===true ) ) === 1 )
  // etc.

www.un.org/Depts/DGACM/index_spanish.htm 如果你不关心计数,或者如果你关心最佳业绩: 那么,为了真相/理智的解决办法,就只能使用双向 x子。

if( !!v1 ^ !!v2 ^ !!v3 ^ !!v4 ^ !!v5 )
  // etc.

此处的大多数拟议方法难以读懂。 不仅没有撰写一些加密和魔法比较或试图评论这些比较,而且只是界定了可变的职能,这种功能是自我解释的:

function either(a: boolean, b: boolean): boolean {
  return (a !== b);
}

或更具有普遍性:

function either(a: any, b: any): boolean {
  return Boolean(a) !== Boolean(b);
}

Then you can use it like this:

assert(either(one, another),  Either one or another, not both );

我找到了这一解决办法,在 Java和大字典上形成XOR。

if( +!!a ^ +!!b )
{
  //This happens only when a is true and b is false or a is false and b is true.
}
else
{
  //This happens only when a is true and b is true or a is false and b is false
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.