<位操作>short、char及位操作<<

来源:互联网 发布:数据采集器作用 编辑:程序博客网 时间:2024/05/13 23:28

   今天与Twonan看了一段位操作的代码,两人晕了半天,最后发现是因为short与char位数不同,所以使用了移位操作。Twonan的代码如下:

<span style="font-size:24px;">   void CMyDlg::OnButton1() {// TODO: Add your control notification handler code hereint shiftBit;USHORT code;UCHAR* ptrInput;ptrInput = new UCHAR;code = 0xaa00;for(int i=0;i<2;i++){shiftBit=i*8;ptrInput[i]=(code)>>shiftBit;}delete ptrInput;TRACE("Input[0]:%x,Input[1]:%x",ptrInput[0],ptrInput[1]);} </span>

输出:Input[0]:0,Input[1]:aa


short占2个字节,char占1个字节,char需要2次存储short的一个值。

  

0 0
原创粉丝点击