操作员〜&在C ++代码中 [英] Operator ~ & in C++ code

查看:59
本文介绍了操作员〜&在C ++代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下代码的帮助。特别是句子(〜(a)& 1)

这意味着什么?



Hi, I need help with the code below. Especially with the sentence (~(a) & 1)
What does it means?

#define Check(a) (~(a) & 1)

#define MY_Check(ts,string)                 
    if (Check(ts))                           
    {                                           
        report_event....
        return sts;                     
    }

推荐答案

1。运算符〜对给定值的二进制表示执行逐位否定。同样,& 也对bvalue的二进制表示进行操作。除非你自己定义二进制表示(例如在一个位字段中),否则没有任何保证可以在编译器上按预期工作,因为编译器不保证对每种类型的变量使用相同的二进制表示。



2.不要使用 #define ,至少不能用于可以轻松编写为函数的目的!



3.第二个宏有几个问题:它缺少行继续符 \ ',所以宏定义将为空,后续行将被解释为实际代码,而不是宏的一部分;此外,返回值 sts 似乎没有定义(虽然 .... 似乎暗示你已经省略了可能定义它的代码。



4.这与C ++无关,它是低级C,实际上你也可能这么低使用汇编程序。如果您打算编写C ++程序,我建议您忘记位运算符和 #define 。如果您有充分的理由坚持使用C语言,请标记问题,因此我们知道不提供有关良好C ++编程的建议。问题1适用于任何一种方式!
1. the operator ~ performs a bitwise negation of the binary representation of a given value. Likewise, & also operates on the binary representation of a bvalue. Unless you define that binary representaion yourself (e. g. in a bit field), there is no gurantee this will work as expected on ever compiler, because a compiler is not guranteed to use the same binary representation for each type of variable.

2. Don't use #define, at least not for a purpose that can easily be written as a function!

3. The second macro has several issues: it's lacking the line continuation character '\', so the macro definition will be empty, and the subsequential lines will be interpreted as actual code, not part of a macro; also, the returned value sts doesn't appear to be defined (although the .... appears to imply you've omitted code that might define it)

4. This has got nothing to do with C++, it's low-level C, in fact so low you might as well use assembler. If you intend to write C++ programs, I suggest you forget about both bit operators and #define. If you have a good reason to stick to C instead, please tag the question as such, so we know not to offer advice on good C++ programming. Issue 1 applies either way!


有两个运营商:



第一个:



There are two operators:

The first one:

~ (bitweise Negation)





~(10)表示( 01)







第二种:



~(10) means (01)



And the second:

The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. 





(01)& (11)指(01)



(01) & (11) means (01)


这篇关于操作员〜&在C ++代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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