如何在C++中實現按位存取_C 語言

來源:互聯網
上載者:User

在我創業的一個項目中,為了節約網路頻寬,因此在網路中傳輸資料需要實現緊湊存取,在國防,科研,航天,軍工等多個領域其實也有類似的需求。
實現緊湊存取,不是按一個位元組一個位元組地存取,而是按位存取。比如一個位元組,我們可以儲存8個bool資訊,廢話少說,直接分享代碼(備忘:裡面的代碼演算法值得最佳化)。

//以下為函數定義 

/***********************************************************************/ /*  函數作用:從buffer讀一個位                    */ /*  參數pBuffer[in]:指定buffer                    */ /*  參數nStart[in]:指定位置                     */ /*  參數nEnd[out]:返回結束位置                    */ /*  參數retByte[out]:返回讀取結果值                 */ /*  返回:void                              */ /***********************************************************************/ void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ byte& retByte );  /***********************************************************************/ /*  函數作用:從指定buffer裡讀任意一段位置資料            */ /*  參數pBuffer[in]:指定buffer                    */ /*  參數nStart[in]:指定位置                     */ /*  參數btLength[in]:讀取長度                    */ /*  參數nEnd[out]:返回結束位置                    */ /*  參數retData[out]:返回讀取結果值,支援任意資料類型        */ /*  返回:void                              */ /***********************************************************************/ template<typename T> void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData );  /***********************************************************************/ /*  函數作用:從指定buffer裡讀取一段字串              */ /*  參數pBuffer[in]:指定buffer                    */ /*  參數nStart[in]:指定位置                     */ /*  參數nCount[in]:字串長度                    */ /*  參數nEnd[out]:返回結束位置                    */ /*  參數pRetData[out]:返回讀取字串結果               */ /*  返回:void                              */ /***********************************************************************/ void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData );    /***********************************************************************/ /*  函數作用:向buffer寫一個位                    */ /*  參數pBuffer[in]:指定buffer                    */ /*  參數btData[in]:需要寫入的值                   */ /*  參數nStart[in]:指定位置                     */ /*  參數nEnd[out]:返回結束位置                    */ /*  返回:void                              */ /***********************************************************************/ void WriteOneBit( byte* pBuffer, byte btData, int nStart, /* out */int& nEnd );  /***********************************************************************/ /*  函數作用:向指定buffer裡寫入任意一段資料             */ /*  參數pBuffer[in]:指定buffer                    */ /*  參數tData[in]:需要寫入的資料,支援任意資料類型          */ /*  參數nStart[in]:指定位置                     */ /*  參數btLength[in]:讀取長度                    */ /*  參數nEnd[out]:返回結束位置                    */ /*  返回:void                              */ /***********************************************************************/ template<typename T> void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd );  /***********************************************************************/ /*  函數作用:向指定buffer裡寫取一段字串              */ /*  參數pBuffer[in]:指定buffer                    */ /*  參數pchar[in]:需要寫入的字串                  */ /*  參數nStart[in]:指定位置                     */ /*  參數nCount[in]:字串長度                    */ /*  參數nEnd[out]:返回結束位置                    */ /*  返回:void                              */ /***********************************************************************/ void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, /* out */int& nEnd ); 


//以下為函數實現

void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ byte& retByte ) {   byte btData = pBuffer[nStart/8];   btData = btData << nStart%8;   retByte = btData >> 7;   nEnd = nStart+1; }  template<typename T> void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData ) {   //順序讀位   retData = 0;   if ( btLength > sizeof(T)*8 )     return ;      byte btData;   T tData;   while ( btLength-- )   {     ReadOneBit(pBuffer, nStart, nStart, btData);     tData = btData << btLength;     retData |= tData;   }      nEnd = nStart; }  void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData ) {   for ( int nIndex=0; nIndex<nCount; nIndex++ )   {     ReadDataFromBuffer(pBuffer, nStart, 8, nStart, pRetData[nIndex]);   }   nEnd = nStart; }   void WriteOneBit( byte* pBuffer, byte btData, int nStart, /* out */int& nEnd ) {   int nSet = nStart / 8;   byte c = pBuffer[nSet];   switch ( btData )   {   case 1:     c |= ( 1 << (7- nStart % 8) );     break;   case 0:     c &= ( ~(1 << (7- nStart % 8) ) );     break;   default:     return;   }   pBuffer [nSet] = c;   nEnd = nStart +1; }    template<typename T> void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd ) { /* //大端機模式   byte btDataLength = sizeof(T);   if ( btLength > sizeof(T)*8 )     return;      int nDataStart = 0; //資料的第一位位置為0,順序寫入   while ( btLength-- )   {     byte bitData;     ReadOneBit((byte*)&tData, nDataStart, nDataStart, bitData);     WriteOneBit(pBuffer, bitData, nStart, nStart);   }      nEnd = nStart; */    //小端機模式:寫buffer的時候,不能順序寫位    //獲得模版佔用位元組大小   byte btDataLength = sizeof(T);    //校正長度是否越界   if ( btLength > sizeof(T)*8 )     return;    //將待寫資料轉為byte*   byte* ptData = (byte*)&tData;     //求模與餘   int nSet = btLength / 8;   int nRin = btLength % 8;      //定義位元組資料與位元據   byte bitData;   byte byteData;   int nTempEnd;    //先寫rin資料   byteData = ptData[nSet];   while ( nRin-- )   {     ReadOneBit(&byteData, 7-nRin, nTempEnd, bitData);     WriteOneBit(pBuffer, bitData, nStart, nStart);   }    //再寫Set資料   while ( nSet )   {     byteData = ptData[--nSet];     //寫一個byte     int i=0;     while ( i!=8 )     {       ReadOneBit(&byteData, i++, nTempEnd, bitData);       WriteOneBit(pBuffer, bitData, nStart, nStart);     }   }   nEnd = nStart;  }   void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, /* out */int& nEnd ) {   for ( int nIndex=0; nIndex<nCount; nIndex++ )   {     WriteDataToBuffer(pBuffer, pchar[nIndex], nStart, 8, nStart);   }   nEnd = nStart; } 

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.