64,649
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int f(int start, int end){
int tmp = (1<<end) -1;
tmp &= ~((1<<start) - 1);
return tmp;
}
int main(){
printf("%020x\n",f(0,8));
printf("%020x\n",f(0,16));
printf("%020x\n",f(16,24));
printf("%020x\n",f(0,31));
}
1 1
2 0 0 0
2 2
4 0 0 0
3 3
8 0 0 0
4 4
10 0 0 0
5 5
20 0 0 0
1 3
e 0 0 0
1 4
1e 0 0 0
1 8
fe 1 0 0
#include <iostream>
#include <cstring>
using namespace std;
void setBits(unsigned char *buffer,int begin,int end)
{
unsigned char *pBeg=&buffer[begin/8];
unsigned char *pEnd=&buffer[end/8];
memset(pBeg,-1,pEnd-pBeg+1); //全部置1
*pBeg&=(-1)<<(begin%8); /*置反*/
*pEnd^=(-1)<<(end%8+1);
}
int main()
{
unsigned char buffer[4]={0};
int i,j;
while(cin>>i>>j)
{
memset(buffer,0,sizeof(buffer));
setBits(buffer,i,j);
for(int i=0;i<4;++i)
{
cout<<hex<<(int)buffer[i]<<" ";
}
cout<<endl;
}
return 0;
}