“变量|变量”是什么意思? C++ 是什么意思?
当我看到这个时,我正在研究 ITE8712 看门狗定时器演示代码:
void InitWD(char cSetWatchDogUnit, char cSetTriggerSignal)
{
OpenIoConfig(); //open super IO of configuration for Super I/O
SelectIoDevice(0x07); //select device7
//set watch dog counter of unit
WriteIoCR(0x72, cSetWatchDogUnit|cSetTriggerSignal);
//CloseIoConfig(); //close super IO of configuration for Super I/O
}
并且,我想知道这行代码的含义:
cSetWatchDogUnit|cSetTriggerSignal
因为 WriteIoCR 函数看起来像这样:
void WriteIoCR(char cIndex, char cData)
{
//super IO of index port for Super I/O
//select super IO of index register for Super I/O
outportb(equIndexPort,cIndex);
//super IO of data for Super I/O
//write data to data register
outportb(equDataPort,cData);
}
所以 cIndex 应该是 0x72,但是 cData 呢?我真的不明白“|”因为我只在条件语句中将它用于 OR (“||”)。
I was looking into this ITE8712 watchdog timer demo code when I saw this:
void InitWD(char cSetWatchDogUnit, char cSetTriggerSignal)
{
OpenIoConfig(); //open super IO of configuration for Super I/O
SelectIoDevice(0x07); //select device7
//set watch dog counter of unit
WriteIoCR(0x72, cSetWatchDogUnit|cSetTriggerSignal);
//CloseIoConfig(); //close super IO of configuration for Super I/O
}
and, I wonder what is meant by this line:
cSetWatchDogUnit|cSetTriggerSignal
because the WriteIoCR function looks like this:
void WriteIoCR(char cIndex, char cData)
{
//super IO of index port for Super I/O
//select super IO of index register for Super I/O
outportb(equIndexPort,cIndex);
//super IO of data for Super I/O
//write data to data register
outportb(equDataPort,cData);
}
So cIndex should be 0x72, but what about the cData? I really don't get the "|" thing as I've only used it for OR ("||") in a conditional statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的,这就是您在这种情况下使用按位或的原因,或者看到它们的使用。
通常,这些变量是用于将多条数据打包到一个字符中的标志
如果
cSetWatchDogUnit
和cSetTriggerSignal
具有非重叠位(想象一下
cSetWatchDogUnit = 1 << 0
和cSetTriggerSignal = 1 << 1
您可以稍后检查看看它们是否是按位设置的,就像这个人为的例子一样:在整个过程中,这两个标志都可以打包到单个字符中并在其中传递,这样,您最终就不会传递布尔数组。 ,您可以添加新常量
cSetSomeOtherDamnfoolThing = 1 << 2
并且您可以在代码中将标志引用为变量。OK, here's why you use a bitwise or, or see them used, in this sort of situation.
Often times, those variables are flags that are used to pack multiple pieces of data into one char
If
cSetWatchDogUnit
andcSetTriggerSignal
have non-overlapping bits (imagine
cSetWatchDogUnit = 1 << 0
andcSetTriggerSignal = 1 << 1
you can check later to see if they are set with a bitwise and, like this contrived example:The whole time, both of these flags can be packed into and passed around in a single char. That way, you don't end up passing an array of bools, you can add new constants
cSetSomeOtherDamnfoolThing = 1 << 2
and you can refer to flags as variables in your code.它是按位
or
,与普通逻辑or
不同。如果设置了任一源变量中的相应位,它基本上会设置目标变量中的位。例如,表达式
43 | 17
可以计算为:请参阅此答案了解更多信息彻底检查各种按位运算符。
当您想要操作数据类型中的特定位时,通常会使用它,例如控制嵌入式系统中的看门狗定时器(您的特定用例)。
您可以使用
或(|)
打开位,使用和(&)
关闭它们(通过反转用于打开它们的位掩码。所以,要打开
b3
位,请使用:要关闭它,请使用:
要检测当前是否设置了
b3
,请使用:您通常会看到位掩码定义了某些内容就像:
或:
至于这意味着什么:
0x72
很可能是您正在写入的某种 I/O 端口,cSetWatchDogUnit
和cSetTriggerSignal
将是您组合起来输出命令的位掩码(设置触发信号并使用看门狗的单位值),可以推断该命令在实践中的含义,但您更安全地参考文档。 而且,如果您不知道看门狗电路的用途,那么它是一个简单的电路,如果您不经常启动它(使用 >另一个命令),它可能会通过激活您正在使用的任何处理器上的重置引脚来重置您的系统。
这是一种自动检测行为不良软件并将设备返回到已知初始状态的方法,它遵循这样的理论:重新启动比继续执行不良行为更好。
It's a bitwise
or
, as distinct to your normal logicalor
. It basically sets the bits in the target variable if the corresponding bit in either of the source variables was set.For example, the expression
43 | 17
can be calculated as:See this answer for a more thorough examination of the various bitwise operators.
It's typically used when you want to manipulate specific bits within a data type, such as control of a watchdog timer in an embedded system (your particular use case).
You can use
or (|)
to turn bits on andand (&)
to turn them off (with the inversion of the bitmask that's used to turn them on.So, to turn on the
b3
bit, use:To turn it off, use:
To detect if
b3
is currently set, use:You'll typically see the bitmasks defined something like:
or:
As to what this means:
More than likely,
0x72
will be an I/O port of some sort that you're writing to andcSetWatchDogUnit
andcSetTriggerSignal
will be bitmasks that you combine to output the command (set the trigger signal and use a unit value for the watchdog). What that command means in practice can be inferred but you're safer referring to the documentation for the watchdog circuitry itself.And, on the off chance that you don't know what a watchdog circuit is for, it's a simple circuit that, if you don't kick it often enough (with another command), it will reset your system, probably by activating the reset pin on whatever processor you're using.
It's a way to detect badly behaving software automatically and return a device to a known initial state, subscribing to the theory that it's better to reboot than continue executing badly.
这是一个按位或。
它在这里用于组合标志。
That's a bitwise or.
It is used here to combine flags.
x | y
通常与 C/C++ 中的普通旧数据一起使用。它的意思是按位或。例如
[注意:
operator |
可以根据您的需要为class/struct
重载。]x | y
is generally used with Plain Old Datas in C/C++. It means bitwise OR.e.g.
[Note:
operator |
can be overloaded forclass/struct
according to your need.]|
是一个按位或。如果任一整数中相同位的一个或另一个打开,则它会将位切换为打开(1 而不是 0)。||
是逻辑 或。如果其中一个或另一个为 true,则返回 true。|
is a bitwise or. It toggles the bits on (1 instead of 0) if one OR the other of the same bit in either integer is on.||
is the logical or. It returns true if one OR the other are true.