实部虚部合并和提取,巧用位运算符<<,>>, |,宏告警
Posted 飞凡可期
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实部虚部合并和提取,巧用位运算符<<,>>, |,宏告警相关的知识,希望对你有一定的参考价值。
#include <iostream>
using namespace std;
#define WARN(eq, str)\\
{\\
if (!(eq))\\
{\\
cout<<"Warning, eq is not satisfied!" << str <<endl;\\
}\\
}
int main()
{
cout << "Hello Complex Digital World!"<<endl;
// create Complex Number: cAll
int aImg = 8, bReal = 4;
int cAll = ( aImg << 16) | bReal;
cout <<hex << " Img:" << aImg << ", Real: "<< bReal <<", Complex: "<< cAll <<endl;
//Obtain Img,Real from cAll
int cImg, cReal;
cImg = cAll >> 16;
cReal = (cAll << 16) >> 16;
cout << " cAll "<< cAll << ", cImg=" << cImg <<", cReal="<< cReal <<endl;
WARN(cImg == 8, "Imag is not 8");
WARN(cReal==16,"Real is not 16");
return 0;
}
result
Hello Complex Digital World!
Img:8, Real: 4, Complex: 80004
cAll 80004, cImg=8, cReal=4
Warning, eq is not satisfied!Real is not 16
以上是关于实部虚部合并和提取,巧用位运算符<<,>>, |,宏告警的主要内容,如果未能解决你的问题,请参考以下文章