今天遇到一个笔试题,愣是没做出来.

winner8080 2010-09-25 10:42:57
写一个函数,要求输入一个二进制数,输入起始位(从右边开始)和转换位数,
从起始位把要求的转换位数按位取反,
比如输入0b0001 0001,起始位是4,位数是3,结果就是0b 0110 0001
函数声明如下:
unsigned reverse(unsigned a[], int pos, int size)

写出实现,

我只写了对a右移pos位,即
unsigned tmp = a >> pos;

但是不知道,怎么对其中的3位取反.就是不知道怎么获得其中的三位.
...全文
759 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
haierpro 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 schoolers 的回复:]
C/C++ code

unsigned reverse(unsigned a, int pos, int size)
{
return a ^ ((~(~0 << size))<<pos);
}
[/Quote]

这个不错!
lz3771 2010-09-27
  • 打赏
  • 举报
回复
和一异或就是取反
bobo364 2010-09-27
  • 打赏
  • 举报
回复
这么些不行,不知为何,希望高手解答
#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;
}
dxms8 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 schoolers 的回复:]
C/C++ code

unsigned reverse(unsigned a, int pos, int size)
{
return a ^ ((~(~0 << size))<<pos);
}
[/Quote]
高端。。学习了
method_01 2010-09-27
  • 打赏
  • 举报
回复
应该是说位运算的。楼主没回答上估计是因为题目没说明白。
乾坤幻影 2010-09-27
  • 打赏
  • 举报
回复
数组是很容易操作的啊!!!
乐CC 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 schoolers 的回复:]
C/C++ code

unsigned reverse(unsigned a, int pos, int size)
{
return a ^ ((~(~0 << size))<<pos);
}
[/Quote]
都能定义成一个宏了,呵呵
greex 2010-09-27
  • 打赏
  • 举报
回复
来个变态版本的:
unsigned char reverse(unsigned char a[], int pos, int size)
{
if(a ==0 || size ==0 )
{
return -1;
}

unsigned char *pTmp = a;
int iAllSize = strlen((const char *)pTmp);
pos = iAllSize -pos-size;
while(size -->0)
{
char ch = a[pos];
if(ch =='0')
{
a[pos] ='1';
}
else
{
a[pos] ='0';
}
pos ++;
}

return 0;
}
szjeff2005 2010-09-27
  • 打赏
  • 举报
回复
unsigned char reverse(unsigned char a, unsigned int pos, unsigned int size)
{
assert((pos+size)<=16);
return a^((0xffff>>(16-len)<<pos);
}
aLosePig 2010-09-27
  • 打赏
  • 举报
回复
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);
}

}
JasonLiu798 2010-09-27
  • 打赏
  • 举报
回复
TCPL 2.9 按位运算符
javasky0 2010-09-26
  • 打赏
  • 举报
回复

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);
}

这要是在考场我也败了
beexie 2010-09-26
  • 打赏
  • 举报
回复

unsigned char change(unsigned char bt,int pos,int size)
{
int bit_width = 8;
unsigned char tmp = 0;
while(size--)
{
tmp += pow(2,pos++);
}
tmp ^= bt;
return tmp;
}
somebody 2010-09-26
  • 打赏
  • 举报
回复
我这里实现的没有考虑二进制前面的标示!
例如0b 0001 0001直接考虑成0001 0001。你可以输入整形都可以了!当然你非要那样,可以转化哈就可以了!很简单的!

我的思想就是把0000 0001(1)右移到你要取反得开始位然后异或。再把1右移到开始的第二位然后异或,这样循环直到你要结束的地方!就ok了!
somebody 2010-09-26
  • 打赏
  • 举报
回复
unsigned reverse(unsigned a, int pos, int size)//pos起始位,size多少位
{
for(int i=0;i<size;i++){
a^=(1<<8-pos-i);
}
return a;
}
Thirty 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 schoolers 的回复:]

C/C++ code

unsigned reverse(unsigned a, int pos, int size)
{
return a ^ ((~(~0 << size))<<pos);
}
[/Quote]呵呵,第一次想出的是我上面的代码,不过19L的代码不错,利用的按位取反一次性完成,比如,就是把需要取反的那几位全部置1,00010001->操作->01110000 所以a^0111000得到最后的结果,喜欢这样的代码
赵4老师 2010-09-26
  • 打赏
  • 举报
回复
假设unsigned类型在内存中的大小为一个字(16位或32位),下面设为32位
·根据pos和size算出pos对应的最右边字偏移量、字内偏移量,pos+size对应的最左边字偏移量、字内偏移量
·将最左边字偏移量到最右边字偏移量中的每个字异或0xFFFFFFFFu,注意最左字内偏移量不为31时需留下最左字不异或,最右字内偏移量不为0时需留下最右字不异或
·根据最左字内偏移量(0~30)将最左字对应异或(0x1u,0x3u,0x7u,...,0x7FFFFFFFu)
·根据最右字内偏移量(1~31)将最右字对应异或(0xFFFFFEu,0xFFFFFFFCu,...,0x80000000u)
Thirty 2010-09-26
  • 打赏
  • 举报
回复
楼主没有指定函数返回类型,而且我觉得参数a[]不对,自己修改了一下

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;
}
Wind_Runner 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lcl_data 的回复:]
本来就是数组啊,那不就简单了吗?
从右边第4位开始 数3位都取反。
1变0 0变1啊
[/Quote]

我觉得这个办法简单可行,

如果没有理解错题意的话
wind_breez 2010-09-26
  • 打赏
  • 举报
回复
unsigned reverse(char a[], int pos, int size)
{
string s(a);
bitset<32> Binum(s);
for (int i = pos-1; i< pos-1+size;i++)
Binum.flip(pos);
return Binum.to_ulong();
}
加载更多回复(12)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧