请问各位:二进制数字反转的算法?(MFC/C++)

violist 2010-09-15 02:34:57
例如把110001011
倒转成110100011
倒转的数字位数不定
请大家给个算法
...全文
539 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
说不明白 2011-08-03
  • 打赏
  • 举报
回复
int reverse(int m)
{
int result = 0;
while(m)
{
result |= m&1;

result<<1;
m>>1;
}
}
libinfei8848 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kartik 的回复:]
我记得STL里有一个很方便的函数来使用,LZ百度下吧
[/Quote]
reserve
Rainqin123 2010-09-16
  • 打赏
  • 举报
回复
不想自己写算法最后的办法就是搞到容器里,让容器自己排序
ForestDB 2010-09-16
  • 打赏
  • 举报
回复
LZ的数据什么类型?
int?char *?
suchx 2010-09-16
  • 打赏
  • 举报
回复
If you only need to reverse a 32 bits words, you can put it in the eax register.
Actualy, the reverse of bits is not needed. You can use another method.
For example, a 8 bits words we need to reverse it.
b7b6b5b4b3b2b1b0=>b0b1b2b3b4b5b6b8
The first bits sequence stands for a unsigned integer(natural) which range in 0..255.
Which we need to find, the seconds bits squence represents a natural which range in 0..255, too. However, it has different weight per bit with the first one.
We can write,
FirstValue =b7*2^7+b6*2^6+...+b1^2+b0
SecondValue=b0*2^7+b1*2^6+...+b6*2+b7
We can write code like this.
 
mov eax,FirstValue
mov ebx,(1 shl 31)
mov SecondValue,0
l1:test eax,1
jz l2
add SecondValue,ebx
l2:shr ebx,1
shr eax,1
jnz l1
东大坡居士 2010-09-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 selooloo 的回复:]
C/C++ code
#include <stdio.h>
int revse32int(int a,int len)//len是要旋转的数字位数,从右侧起
{
int tmp=0,i,j;
for(i=0,j=len-1;i<len;++i,--j)
{
if((a>>i)&1)
tmp|=((a>>i)&1……
[/Quote]

顶个~~~~~~~~~~
mymtom 2010-09-15
  • 打赏
  • 举报
回复
strreverse不可以吗?
赵4老师 2010-09-15
  • 打赏
  • 举报
回复
strrev
xy_zhang 2010-09-15
  • 打赏
  • 举报
回复
倒转的数字位数不定,那么这个数是个什么格式呢?

如果本来就是个0和1的字符串,要求输出也是字符串,那就按照字符串反转的规则。

如果给的是个数字,要求输出也是数字,比如一个int,那就按照上面反转比特的算法得到一个数,然后把低位多出来的0去掉(看最低位,如果为0就右移1位,如果为1就结束循环,然后输出)。
selooloo 2010-09-15
  • 打赏
  • 举报
回复
#include <stdio.h>
int revse32int(int a,int len)//len是要旋转的数字位数,从右侧起
{
int tmp=0,i,j;
for(i=0,j=len-1;i<len;++i,--j)
{
if((a>>i)&1)
tmp|=((a>>i)&1)<<j;
}
return tmp;

}
int main(void)
{
int a=0x18b;

printf("%x\n",a);
printf("%x\n",revse32int(a,9));
getchar();
return 0;
}
evoloyeu 2010-09-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 violist 的回复:]

我看了,可是那个都得要知道位数吧?
我位数不确定的
[/Quote]
你要翻转的是一个什么样类型的数据呢?
violist 2010-09-15
  • 打赏
  • 举报
回复
我看了,可是那个都得要知道位数吧?
我位数不确定的
evoloyeu 2010-09-15
  • 打赏
  • 举报
回复
参考这个:
int revse32int(int a)
{
int tmp=0;
a = a<<8;
tmp |= ((a & 0xff000000)>>24);
tmp |= ((a & 0x00ff0000)>>8);
tmp |= ((a & 0x0000ff00)<<8);
tmp |= ((a & 0x000000ff)<<24);
return tmp;

}
harderman 2010-09-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wcg_jishuo 的回复:]
1、倒转的位数要定。strlen(str)
2、然后怎么倒用指针。或者第一位与最后一位想与。第二位与倒数第二位想与。
[/Quote]
+1
kartik 2010-09-15
  • 打赏
  • 举报
回复
我记得STL里有一个很方便的函数来使用,LZ百度下吧
AIGPTchina 2010-09-15
  • 打赏
  • 举报
回复
1、倒转的位数要定。strlen(str)
2、然后怎么倒用指针。或者第一位与最后一位想与。第二位与倒数第二位想与。
weixiaoshashou 2010-09-15
  • 打赏
  • 举报
回复
在一个循环中位移不知可行否。

69,370

社区成员

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

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