What do 0x0040 and pipe sign denote here?

我们两清 提交于 2019-12-13 20:28:38

问题


Here SIMPLE_EX2 is being ORed with 0x0040 and the whole this providing as an address to SIMPLE_EX1. Is my understanding correct?

#define SIMPLE_EX1  (0x0040 | SIMPLE_EX2) 

回答1:


| is not a pipe sign in C. It's a bit-wise or. So this expression:

0x0040 | SIMPLE_EX2

Simply gets the value of SIMPLE_EX2 and sets it's 7th bit (from right) to 1.


Unlikely, but note that if SIMPLE_EX2 itself is an expression with an operator that has lower precedence than |, the overall expression may be interpreted wrongly. For example if SIMPLE_EX2 is a?b:c, then SIMPLE_EX1 becomes (0x0040|a)?b:c which is not what I wrote above.




回答2:


You should read a good C programming book (if you are learning C), or a good C++ programming book if you are learning C++.

Assuming SIMPLE_EX2 is #define-d as a constant integer, or a constant integer expression in parenthesis, then SIMPLE_EX1is that integer bit-or-ed with the 0x0040 hexadecimal constant (ie 64 in decimal, or 0b1000000 in binary).




回答3:


SIMPLE_EX2 is being ORed with 0x0040

yes.

and the whole this providing as an address to SIMPLE_EX1.

no.

The #define preprocessor directive is basically a find-and-replace text operation which is done before compilation. Nothing more and noting less. So whenever you write SIMPLE_EX1 in your code, it is textually replaced with (0x0040 | SIMPLE_EX2) before compilation.

Interesting code snippet which illustrates this:

#define SIX 1+5
#define NINE 8+1

printf("Six times nine is %d.", SIX * NINE);

This code will return 42, not 54 like one would expect, because the preprocessor turns the whole program into:

printf("Six times nine is %d.", 1 + 5 * 8 + 1);



回答4:


It is just setting the 7th bit from right for SIMPLE_EX2 and assigning it to SIMPLE_EX1



来源:https://stackoverflow.com/questions/12278044/what-do-0x0040-and-pipe-sign-denote-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!