理解 C++ 中的 "Bitwise-And (&)" 和 "Unary complement(~)"
Understanding "Bitwise-And (&)" and "Unary complement(~)" in c++
我在理解 Bitwise-And
和 Unary Complement
时遇到了一些困难,因为它们都用于此代码段
if((oldByte==m_DLE) & (newByte==m_STX)) {
int data_index=0;
//This below line --- does it returns true if both the oldByte and newByte are not true
//and within timeout
while((timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))) {
if(Serial.available()>0) {
oldByte=newByte;
newByte=Serial.read();
if(newByte==m_DLE) {
.
.
.
这两个运算符是否 & ~
正在执行逻辑非运算,例如检查 oldByte
和 newByte
是否都为假
以上代码来自link --> 代码
的第227行
我正在尝试使用 C 语言为我的应用程序实现代码,但没有计时功能
if((oldByte==DLE) && (newByte== STX)) {
data_index = 0;
// is this the correct implematation for above C++ code to C
while(! ((oldByte== DLE) && (newByte== ETX))){
oldByte = newByte;
此方法在 C 中实现是否正确
由于相等运算符 (==) 的结果为 0 或 1,因此您也可以使用按位与。 (foo==1) & ~(bar==1) 也有效,因为与 (foo==1) 的 AND 总是产生 1 和 0,它屏蔽了 ~(bar==1) 中的所有其他位。但是,强烈建议使用逻辑对应物 &&、||和 !.
以下将无法按预期工作:
if (~(bar == 1) & ~(foo == 1))
例如如果 foo = bar = 1,那么它在 ia32 上的计算结果为 0xfffffffe,它与 0 不同,因此 "TRUE"
(timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))
等同于(但可能不如)
(timeout.read_s()<m_timeout) && !(oldByte==m_DLE && newByte==m_ETX)
相当于(IMO 的可读性低于)
(timeout.read_s()<m_timeout) && (oldByte!=m_DLE || newByte!=m_ETX)
编辑:应该添加关于短路的警告。虽然特定的示例语句将全部 return 相同的值,但使用 && 或 ||将跳过评估不会影响结果的部分。这在您的特定示例中并不重要,但在这样的示例中可能非常重要:
(oldByte!=nullptr & *oldByte == m_ETX) // will crash when oldByte=nullptr.
(oldByte!=nullptr && *oldByte == m_ETX) // will evaluate to false when oldByte=nullptr.
我在理解 Bitwise-And
和 Unary Complement
时遇到了一些困难,因为它们都用于此代码段
if((oldByte==m_DLE) & (newByte==m_STX)) {
int data_index=0;
//This below line --- does it returns true if both the oldByte and newByte are not true
//and within timeout
while((timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))) {
if(Serial.available()>0) {
oldByte=newByte;
newByte=Serial.read();
if(newByte==m_DLE) {
.
.
.
这两个运算符是否 & ~
正在执行逻辑非运算,例如检查 oldByte
和 newByte
是否都为假
以上代码来自link --> 代码
的第227行我正在尝试使用 C 语言为我的应用程序实现代码,但没有计时功能
if((oldByte==DLE) && (newByte== STX)) {
data_index = 0;
// is this the correct implematation for above C++ code to C
while(! ((oldByte== DLE) && (newByte== ETX))){
oldByte = newByte;
此方法在 C 中实现是否正确
由于相等运算符 (==) 的结果为 0 或 1,因此您也可以使用按位与。 (foo==1) & ~(bar==1) 也有效,因为与 (foo==1) 的 AND 总是产生 1 和 0,它屏蔽了 ~(bar==1) 中的所有其他位。但是,强烈建议使用逻辑对应物 &&、||和 !.
以下将无法按预期工作:
if (~(bar == 1) & ~(foo == 1))
例如如果 foo = bar = 1,那么它在 ia32 上的计算结果为 0xfffffffe,它与 0 不同,因此 "TRUE"
(timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))
等同于(但可能不如)
(timeout.read_s()<m_timeout) && !(oldByte==m_DLE && newByte==m_ETX)
相当于(IMO 的可读性低于)
(timeout.read_s()<m_timeout) && (oldByte!=m_DLE || newByte!=m_ETX)
编辑:应该添加关于短路的警告。虽然特定的示例语句将全部 return 相同的值,但使用 && 或 ||将跳过评估不会影响结果的部分。这在您的特定示例中并不重要,但在这样的示例中可能非常重要:
(oldByte!=nullptr & *oldByte == m_ETX) // will crash when oldByte=nullptr.
(oldByte!=nullptr && *oldByte == m_ETX) // will evaluate to false when oldByte=nullptr.