69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
int reverse(char *a, int pos, int size);
int main()
{
char a[9]="0b00010001";
char *p=a;
reverse(p,4,3);
printf("%s\n",p);
system("pause");
return 0;
}
int reverse(char *a, int pos, int size)
{
int i;
for(i=pos-1;i<size;i++)
{
if(a[i]==0)
{
a[i]=1;
}
else
{
a[i]=0;
}
}
return 0;
}
unsigned reverse(unsigned a[], int pos, int size)
{
unsigned int uiTag ;
unsigned int *puiTmp;
assert(a != NULL && (pos >=0 && pos < sizeof(unsigned int)) && (size > 0 && size < sizeof(unsigned int)));
uiTag = 1 << pos; /* 把1移到起始位置*/
puiTmp = a;
while (size-- > 0)
{
/* 与1异或的位会翻转 */
(*puiTmp) ^= uiTag;
/* 移到下一位 */
uiTag <<= 1;
}
return (*puiTmp);
}
void test_reverse_pos(void)
{
unsigned int Input = 0x0b11;
unsigned int Expect = 0x0b61;
reverse(&Input, 4, 3);
if (Input == Expect)
{
puts("pass test_reverse_pos \n");
}
else
{
printf(" No pass test_reverse_pos Output =%x expect = %x\n", Input, Expect);
}
}
char reverse(char a,int pos,int size){
assert(pos>=0&&size>=0&&pos+size<=8);
char mid=(a>>pos)&((1<<size)-1);
char r_mid=(~mid)&((1<<size)-1);
return a-(mid<<pos)+(r_mid<<pos);
}
unsigned char reverse(unsigned char a, int pos, int size)
{
unsigned char i,mask;
for(i=0;i<size;i++)
{
mask=0x1<<(pos+i);
a=a^mask;
}
return a;
}