国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

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

2020-05-23 14:17:00
字體:
來源:轉載
供稿:網友

實現緊湊存取,不是按一個字節一個字節地存取,而是按位存取,本文就是介紹了如何在C++中實現按位存取,需要的朋友可以參考下

在我創業的一個項目中,為了節約網絡帶寬,因此在網絡中傳輸數據需要實現緊湊存取,在國防,科研,航天,軍工等多個領域其實也有類似的需求。

實現緊湊存取,不是按一個字節一個字節地存取,而是按位存取。比如一個字節,我們可以存儲8個bool信息,廢話少說,直接分享代碼(備注:里面的代碼算法值得優化)。

//以下為函數定義

 

 
  1. /***********************************************************************/ 
  2. /* 函數作用:從buffer讀一個位 */ 
  3. /* 參數pBuffer[in]:指定buffer */ 
  4. /* 參數nStart[in]:指定位置 */ 
  5. /* 參數nEnd[out]:返回結束位置 */ 
  6. /* 參數retByte[out]:返回讀取結果值 */ 
  7. /* 返回:void */ 
  8. /***********************************************************************/ 
  9. void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ byte& retByte );  
  10.  
  11. /***********************************************************************/ 
  12. /* 函數作用:從指定buffer里讀任意一段位置數據 */ 
  13. /* 參數pBuffer[in]:指定buffer */ 
  14. /* 參數nStart[in]:指定位置 */ 
  15. /* 參數btLength[in]:讀取長度 */ 
  16. /* 參數nEnd[out]:返回結束位置 */ 
  17. /* 參數retData[out]:返回讀取結果值,支持任意數據類型 */ 
  18. /* 返回:void */ 
  19. /***********************************************************************/ 
  20. template<typename T>  
  21. void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData );  
  22.  
  23. /***********************************************************************/ 
  24. /* 函數作用:從指定buffer里讀取一段字符串 */ 
  25. /* 參數pBuffer[in]:指定buffer */ 
  26. /* 參數nStart[in]:指定位置 */ 
  27. /* 參數nCount[in]:字符串長度 */ 
  28. /* 參數nEnd[out]:返回結束位置 */ 
  29. /* 參數pRetData[out]:返回讀取字符串結果 */ 
  30. /* 返回:void */ 
  31. /***********************************************************************/ 
  32. void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData );  
  33.  
  34.  
  35.  
  36. /***********************************************************************/ 
  37. /* 函數作用:向buffer寫一個位 */ 
  38. /* 參數pBuffer[in]:指定buffer */ 
  39. /* 參數btData[in]:需要寫入的值 */ 
  40. /* 參數nStart[in]:指定位置 */ 
  41. /* 參數nEnd[out]:返回結束位置 */ 
  42. /* 返回:void */ 
  43. /***********************************************************************/ 
  44. void WriteOneBit( byte* pBuffer, byte btData, int nStart, /* out */int& nEnd );  
  45.  
  46. /***********************************************************************/ 
  47. /* 函數作用:向指定buffer里寫入任意一段數據 */ 
  48. /* 參數pBuffer[in]:指定buffer */ 
  49. /* 參數tData[in]:需要寫入的數據,支持任意數據類型 */ 
  50. /* 參數nStart[in]:指定位置 */ 
  51. /* 參數btLength[in]:讀取長度 */ 
  52. /* 參數nEnd[out]:返回結束位置 */ 
  53. /* 返回:void */ 
  54. /***********************************************************************/ 
  55. template<typename T>  
  56. void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd );  
  57.  
  58. /***********************************************************************/ 
  59. /* 函數作用:向指定buffer里寫取一段字符串 */ 
  60. /* 參數pBuffer[in]:指定buffer */ 
  61. /* 參數pchar[in]:需要寫入的字符串 */ 
  62. /* 參數nStart[in]:指定位置 */ 
  63. /* 參數nCount[in]:字符串長度 */ 
  64. /* 參數nEnd[out]:返回結束位置 */ 
  65. /* 返回:void */ 
  66. /***********************************************************************/ 
  67. void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, /* out */int& nEnd ); 

//以下為函數實現

 

 
  1. void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ byte& retByte )  
  2. {  
  3. byte btData = pBuffer[nStart/8];  
  4. btData = btData << nStart%8;  
  5. retByte = btData >> 7;  
  6. nEnd = nStart+1;  
  7. }  
  8.  
  9. template<typename T>  
  10. void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData )  
  11. {  
  12. //順序讀位  
  13. retData = 0;  
  14. if ( btLength > sizeof(T)*8 )  
  15. return ;  
  16.  
  17. byte btData;  
  18. T tData;  
  19. while ( btLength-- )  
  20. {  
  21. ReadOneBit(pBuffer, nStart, nStart, btData);  
  22. tData = btData << btLength;  
  23. retData |= tData;  
  24. }  
  25.  
  26. nEnd = nStart;  
  27. }  
  28.  
  29. void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData )  
  30. {  
  31. for ( int nIndex=0; nIndex<nCount; nIndex++ )  
  32. {  
  33. ReadDataFromBuffer(pBuffer, nStart, 8, nStart, pRetData[nIndex]);  
  34. }  
  35. nEnd = nStart;  
  36. }  
  37.  
  38.  
  39. void WriteOneBit( byte* pBuffer, byte btData, int nStart, /* out */int& nEnd )  
  40. {  
  41. int nSet = nStart / 8;  
  42. byte c = pBuffer[nSet];  
  43. switch ( btData )  
  44. {  
  45. case 1:  
  46. c |= ( 1 << (7- nStart % 8) );  
  47. break;  
  48. case 0:  
  49. c &= ( ~(1 << (7- nStart % 8) ) );  
  50. break;  
  51. default:  
  52. return;  
  53. }  
  54. pBuffer [nSet] = c;  
  55. nEnd = nStart +1;  
  56. }  
  57.  
  58.  
  59.  
  60. template<typename T>  
  61. void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd )  
  62. {  
  63. /* //大端機模式  
  64. byte btDataLength = sizeof(T);  
  65. if ( btLength > sizeof(T)*8 )  
  66. return;  
  67.  
  68. int nDataStart = 0; //數據的第一位位置為0,順序寫入  
  69. while ( btLength-- )  
  70.  
  71. byte bitData;  
  72. ReadOneBit((byte*)&tData, nDataStart, nDataStart, bitData);  
  73. WriteOneBit(pBuffer, bitData, nStart, nStart);  
  74.  
  75.  
  76. nEnd = nStart;  
  77. */ 
  78.  
  79. //小端機模式:寫buffer的時候,不能順序寫位  
  80.  
  81. //獲得模版占用字節大小  
  82. byte btDataLength = sizeof(T);  
  83.  
  84. //校驗長度是否越界  
  85. if ( btLength > sizeof(T)*8 )  
  86. return;  
  87.  
  88. //將待寫數據轉為byte*  
  89. byte* ptData = (byte*)&tData;  
  90.  
  91. //求模與余  
  92. int nSet = btLength / 8;  
  93. int nRin = btLength % 8;  
  94.  
  95. //定義字節數據與位數據  
  96. byte bitData;  
  97. byte byteData;  
  98. int nTempEnd;  
  99.  
  100. //先寫rin數據  
  101. byteData = ptData[nSet];  
  102. while ( nRin-- )  
  103. {  
  104. ReadOneBit(&byteData, 7-nRin, nTempEnd, bitData);  
  105. WriteOneBit(pBuffer, bitData, nStart, nStart);  
  106. }  
  107.  
  108. //再寫Set數據  
  109. while ( nSet )  
  110. {  
  111. byteData = ptData[--nSet];  
  112. //寫一個byte  
  113. int i=0;  
  114. while ( i!=8 )  
  115. {  
  116. ReadOneBit(&byteData, i++, nTempEnd, bitData);  
  117. WriteOneBit(pBuffer, bitData, nStart, nStart);  
  118. }  
  119. }  
  120. nEnd = nStart;  
  121.  
  122. }  
  123.  
  124.  
  125. void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, /* out */int& nEnd )  
  126. {  
  127. for ( int nIndex=0; nIndex<nCount; nIndex++ )  
  128. {  
  129. WriteDataToBuffer(pBuffer, pchar[nIndex], nStart, 8, nStart);  
  130. }  
  131. nEnd = nStart;  
  132. }  

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 寻乌县| 湾仔区| 星座| 盐城市| 修水县| 天长市| 黑水县| 东源县| 略阳县| 灵川县| 扶沟县| 聊城市| 平阴县| 绥中县| 正镶白旗| 炉霍县| 乌拉特中旗| 乌鲁木齐县| 黎城县| 南投市| 洪湖市| 镇巴县| 宜都市| 手游| 琼中| 咸丰县| 鄄城县| 三亚市| 栾城县| 商都县| 政和县| 乌海市| 宁海县| 营口市| 泾阳县| 万年县| 大邑县| 财经| 古交市| 濉溪县| 凭祥市|