使用xor运算符进行布尔检查是否是一种好习惯? [英] Is it good practice to use the xor operator for boolean checks?

查看:49
本文介绍了使用xor运算符进行布尔检查是否是一种好习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我个人喜欢 exclusive或 ^运算符,因为它的简洁性使其在布尔检查的上下文中有意义.我更喜欢写

I personally like the exclusive or, ^, operator when it makes sense in the context of boolean checks because of its conciseness. I much prefer to write

if (boolean1 ^ boolean2)
{
  //do it
}

if((boolean1 && !boolean2) || (boolean2 && !boolean1))
{
  //do it
}

但是我经常会从其他经验丰富的Java开发人员(不仅是新手)中获得困惑的外观,有时还会评论如何仅将其用于按位运算.

but I often get confused looks from other experienced Java developers (not just the newbies), and sometimes comments about how it should only be used for bitwise operations.

我对使用^运算符的最佳做法感到好奇.

I'm curious as to the best practices regarding the usage of the ^ operator.

推荐答案

您可以直接使用!=.

这篇关于使用xor运算符进行布尔检查是否是一种好习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆