切换变量中的位 [英] Toggle bits in a variable

查看:83
本文介绍了切换变量中的位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何才能使用最短的代码切换变量中的所有位?

How can we just toggle all the bits in a variable using the shortest code?

推荐答案

使用〜"-不能按位.参见
此处. [
Use "~" - bitwise not. See
here.[^]
int newInvertedVariable = ~oldVariable;


XOR运算符(^)可用于切换一位.
XOR operator(^) can be used to toggle a bit.
number ^= 1 << x;


在此处查看更多信息:
有点切换 [


Look here for more: Toggling a bit[^]


您应该查看显示的信息 ^ ].
You should review the information presented here[^].


这篇关于切换变量中的位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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