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

本文档展示了如何使用C++中的#define宏定义WARN来实现条件警告输出,并通过位操作创建和解析复合数字。示例代码中,创建了一个16位复合数,并从复合数中提取出图像部分和实数部分。最后,验证了提取的值是否正确,输出了警告信息。
摘要由CSDN通过智能技术生成
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值