2-6,2-7,2-8
void main()
{
unsigned setbit(unsigned,int,int,unsigned);
unsigned invert(unsigned,int,int);
unsigned rightrot(unsigned,int);
unsigned mm,o,p;
mm=setbit(31,3,2,45);
o=invert(31,3,2);
p=rightrot(3,1);
cout<<mm<<endl<<o<<endl<<p<<endl;
}
unsigned setbit(unsigned x,int p,int n,unsigned y)
{
unsigned temp;
temp=(~0<<p+1)|~(~0<<p+1-n);
x=x&temp;
y=(y<<p+1-n)&~temp;
return x|y;
}
unsigned invert(unsigned x,int p,int n)
{
unsigned temp,y;
temp=(~0<<p+1)|~(~0<<p+1-n);
y=~x;
x=x&temp;
y=y&~temp;
return x|y;
}
//求机器字长
int wordlength()
{
int i;
unsigned v=(unsigned)~0;
for(i=1;(v=v>>1)>0;++i)
;
return i;
}
unsigned rightrot(unsigned x,int n)
{
unsigned N;
N=wordlength();
cout<<N<<endl;
//x=(x<<(N-n))|(x>>n);
/*while((n=n%wordlength())>0)*/
{
unsigned rbits;
rbits=~(~0<<n)&x;
rbits=rbits<<(N-n);
x=x>>n;
x=x|rbits;
}
return x;
}
2-9,2-10
int bitcount(unsigned x)
{
int b;
/*for(b=0;x!=0;x&=(x-1))
++b;*/
for(b=0;x!=0;x>>=1)
if(x&01)
++b;
return b;
}
*************************************************************
int lower(int c)
{
if(c>='A'&&c<='Z')
return c+'a'-'A';
else
return c;
//return (c>='A'&&c<='Z')?(c+'a'-'A'):c;
}
3-1,3-2
//折半查找
int binsearch(int x,int v[],int n)
{
int low,high,mid;
low=0;
high=n-1;
/*while(low<=high)
{
mid=(low+high)/2;
if(x<v[mid])
high=mid+1;
else if(x>v[mid])
low=mid+1;
else
return mid;
}
return -1;*/
mid=(low+high)/2;
while(low<=high&&x!=v[mid])
{
if(x<v[mid])
high=mid+1;
else
low=mid-1;
}
if(x==v[mid])
return mid;
return -1;
}
**********************************************************************
void main()
{
void escape(char s[],char t[]);
char ss[1000]="i am o boy!";
char tt[1000]=" are you o!";
escape(ss,tt);
cout<<ss<<endl;
}
void escape(char ss[],char tt[])
{
int i;
int length;
//length=strlen(ss);
for(length=0;ss[length]!='\0';++l
cc语言习题 来自淘豆网m.daumloan.com转载请标明出处.