64,649
社区成员
发帖
与我相关
我的任务
分享
DWORD WINAPI CMyDlg::CountBit(LPVOID lpParameter)
{
DWORD dwRead=0;
std::fstream mystream;
mystream.open(m_filename,std::ios::binary|std::ios::in);
char str[4096];
while(mystream.getline(str,4096))
{
std::string s(str[0],4096);//这里有问题,暂时没有想好
std::bitset<4096> f(s);
dwRead += f.count();
}
return dwRead;
}
//这个按钮点击就会进行计算
void CMyDlg::OnBnClickedCalc()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE hThread;
hThread = CreateThread(NULL,0,CountBit,NULL,0,NULL);
CString str;
str.Format(L"比特数:%d,耗时:%ms,文件名:%d",CountBit(0),0,m_filename);
CloseHandle(hThread);
}
char buf[4096] = {5};
buf[2] = -1;
buf[4095] = 5;
unsigned int *p = (unsigned int *)buf;
unsigned int *pend = p + 1024;
unsigned int tmp;
unsigned int sum = 0;
while(p < pend)
{
tmp = *p;
tmp = (tmp & 0x55555555) + ((tmp>>1) & 0x55555555);
tmp = (tmp & 0x33333333) + ((tmp>>2) & 0x33333333);
tmp = (tmp & 0x0f0f0f0f) + ((tmp>>4) & 0x0f0f0f0f);
tmp = (tmp & 0x00ff00ff) + ((tmp>>8) & 0x00ff00ff);
tmp = (tmp & 0x0000ffff) + ((tmp>>16) & 0x0000ffff);
sum += tmp;
++p;
}
cout<<sum;
/*
计算整数x的二进制中含有多少个1
*/
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
int count = 0;
int x;
scanf("%d",&x);
while(x)
{
count++;
x = x&x-1;
}
printf("total: %d\n",count);
return 0;
}
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int byte;
while( (byte=fgetc(stdin))!=EOF ) //假设就从终端输入字符串当做二进制得了,注意'0'对应的byte是48:
{
if(byte=='\n')
{
break;
}
//转化为unsigned char,然后隐式转化为unsigned long,防止有符号数扩宽时符号位填充1,这是你读二进制文件必须注意的
//但是,这里我限制bitset是8位,所以不注意也没关系。
bitset<8> cnt((unsigned char)byte);
cout<<cnt.count()<<" ";
}
cout<<endl;
return 0;
}
#include<stdio.h>
#include<stdlib.h>
unsigned char numTable[256] =
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
};
int main()
{
FILE *pf;
int ch;
unsigned int total_num = 0;
if (NULL == (pf = fopen("in.bin", "rb")))
{
printf("%s\n", "Can not find the file!");
exit(1);
}
while ((ch = fgetc(pf)) != EOF)
{
total_num += numTable[ch];
}
printf("%d\n", total_num);
system("pause");
return 0;
}
//这个按钮点击就会进行计算
void CMyDlg::OnBnClickedCalc()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE hThread;
hThread = CreateThread(NULL,0,CountBit,NULL,0,NULL);//这里创建一个线程
CString str;
str.Format(L"比特数:%d,耗时:%ms,文件名:%d",CountBit(0),0,m_filename);//这里我用到 了CountBit函数我不知道这里对不对
CloseHandle(hThread);
}