实部虚部合并和提取,巧用位运算符<<,>>, |,宏告警

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

以上是关于实部虚部合并和提取,巧用位运算符<<,>>, |,宏告警的主要内容,如果未能解决你的问题,请参考以下文章

复数基础知识

复数是啥啊,为啥C=a+bi

编程思想:巧用位运算重构代码

python二级练习和考试复习(将复数2.3103-1.3410-3j赋值给变量A,并分别提取A的实部和虚部。)

c++简单复数计算器

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算