如何在C++中實現按位存取
在我創(chuàng)業(yè)的一個項目中,為了節(jié)約網絡帶寬,因此在網絡中傳輸數據需要實現緊湊存取,在國防,科研,航天,軍工等多個領域其實也有類似的需求。
實現緊湊存取,不是按一個字節(jié)一個字節(jié)地存取,而是按位存取。比如一個字節(jié),我們可以存儲8個bool信息,廢話少說,直接分享代碼(備注:里面的代碼算法值得優(yōu)化)。
//以下為函數定義
/***********************************************************************/ /* 函數作用:從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的時候,不能順序寫位 //獲得模版占用字節(jié)大小 byte btDataLength = sizeof(T); //校驗長度是否越界 if ( btLength > sizeof(T)*8 ) return; //將待寫數據轉為byte* byte* ptData = (byte*)&tData; //求模與余 int nSet = btLength / 8; int nRin = btLength % 8; //定義字節(jié)數據與位數據 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; }
以上就是本文的全部內容,希望對大家的學習有所幫助。
您可能感興趣的文章
- 04-02c語言沒有round函數 round c語言
- 01-10如何判斷一個數是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現全排列算法的方法詳解
- 01-10如何判斷一個數是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10如何查看進程實際的內存占用情況詳解
- 01-10深入解析最長公共子串
- 01-10c++中inline的用法分析
- 01-10如何尋找數組中的第二大數
- 01-10用C++實現DBSCAN聚類算法


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 01-11ajax實現頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結