- /*
- *CaculateOnes.cpp
- *Powered by Keamou
- */
- #include <iostream.h>
- int start;
- int end;
- int ones;
- void main()
- {
- char obj[2]={'d','a'};
- char temp = 1;
- int objlenth;
- int lenth;
- int num;
- num = 2;
- lenth = 8;
- objlenth=lenth*num;
- //////////////////////////////////////////////////////////////////////////
- //顯示01串
- //////////////////////////////////////////////////////////////////////////
- for (int i=0;i<objlenth;i++)
- {
- if ((obj[i/lenth]&(temp<<(i%lenth)))==0)
- {
- cout<<"1";
- }
- else
- {
- cout<<"0";
- };
- }
- cout<<endl;
- //////////////////////////////////////////////////////////////////////////
- //以下是計算上述01串中的1序列
- //
- //通過位操作來讀取01值
- //////////////////////////////////////////////////////////////////////////
- for (i=0;i<objlenth;i++)
- {
- if ((obj[i/lenth]&(temp<<(i%lenth)))==0)
- {
- start=i;
- ones=0;
- do
- {
- ones++;
- i++;
- }while ((obj[i/lenth]&(temp<<(i%lenth)))==0 && i<objlenth);
- end=i-1;
- cout<<"start:/t"<<start<<"/tend:/t"<<end<<"/tlenth:/t"<<ones<<endl;
- }
- }
- cout<<endl;
- }
計算數組的二進制表示的01個數
以下是自寫的一個比較簡單的計算二進制中1的個數及其起始和終止位置的程序,這個程序可擴展成對一個數組的計算。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.