假设一个数为ox01010101,我想求出ox01000000,用最快速的算法

mxfeng 2013-01-02 06:53:54
RT-----------------------------------
...全文
372 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
小布 2013-01-04
  • 打赏
  • 举报
回复
与0x01000000相与 0x01010101&0x01000000
千树之影 2013-01-04
  • 打赏
  • 举报
回复
a=0x01000000
jimette 2013-01-04
  • 打赏
  • 举报
回复
引用 10 楼 qq120848369 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849[root@vps616 c]# ./main quick_proc(0x0)=0x0quick_proc(0x1)=0x1quick_proc(0x2)=0x2quick_……
mark
孤独小剑 2013-01-04
  • 打赏
  • 举报
回复
static int get_only_first_one(int x)
{
    int r;
    do {
        r = x;
        x = x & (x - 1);
    } while(x);
    return r;
}
这样改更好些!
孤独小剑 2013-01-04
  • 打赏
  • 举报
回复
#include <stdio.h>

static int get_only_first_one(int x)
{
	int r = x;
	while(x & (x-1)) {
		x = x & (x - 1);
		r = x;
	}
	return r;
}

int main(int argg, char *argv[])
{
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(1));//1
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(2));//2
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(3));//2
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(4));//4
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(5));//4
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(6));//4
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(7));//4
	fprintf(stdout, "0x%08X ...\n", get_only_first_one(8));//8
	return 0;
}
其他情况(0、32位、64位等)楼主自己变通吧。
赵4老师 2013-01-04
  • 打赏
  • 举报
回复
int d=0x01010101,r;
__asm {
 mov eax,d
 and eax,0x11000000
 mov r,eax
}
printf("0x%08x\n",r);
prajna 2013-01-03
  • 打赏
  • 举报
回复
#include <iostream>
#define BIT(n) (1<<(n))
static unsigned int _msb(unsigned int n, int l, int r)
{
	int m = (l+r)/2;
	if (l==r) return l;
	if (n > BIT(m)) l=m+1;
	else r=m;
	return _msb(n, l, r);
}
unsigned int msb(unsigned int n)
{
	return n?(1<<_msb(n, 0, 8*sizeof(n)-1)):0;
}
int main(void)
{
	std::cout << 0 <<": "<< msb(0) << std::endl;
	for (int i=0; i<32; i++){
		std::cout << i <<": "<< msb(1<<i) << std::endl;
	}
	return 0;
}
AI.wanderer 2013-01-03
  • 打赏
  • 举报
回复
0x01010101&0x01000000
qq120848369 2013-01-03
  • 打赏
  • 举报
回复
[root@vps616 c]# ./main 
quick_proc(0x0)=0x0
quick_proc(0x1)=0x1
quick_proc(0x2)=0x2
quick_proc(0x3)=0x2
quick_proc(0x4)=0x4
quick_proc(0x5)=0x4
quick_proc(0x6)=0x4
quick_proc(0x7)=0x4
quick_proc(0x8)=0x8
quick_proc(0x9)=0x8
quick_proc(0xa)=0x8
quick_proc(0xb)=0x8
quick_proc(0xc)=0x8
quick_proc(0xd)=0x8
quick_proc(0xe)=0x8
quick_proc(0xf)=0x8
quick_proc(0x10)=0x10
quick_proc(0x11)=0x10
quick_proc(0x12)=0x10
quick_proc(0x13)=0x10

[root@vps616 c]# cat main.c
#include <stdio.h>

int quick_proc(int n) {
    int off = 0;
    unsigned int un = (unsigned int)n;
    
    while (un) {
        un >>= 1;
        ++ off;
    }

    if (off) {
        return 1 << (off - 1);
    }
    return 0;
}

int main(int argc, char* const argv[]) {
#ifdef DEBUG
    int n;
    for (n = 0; n != 20; ++ n) {
        printf("quick_proc(0x%x)=0x%x\n", (unsigned int)n, (unsigned int)quick_proc(n));
    }
#endif
    return 0;
}
mxfeng 2013-01-03
  • 打赏
  • 举报
回复
我是楼主 ********************************************************************* 我只想保留第一个 最高位是1的那个位,其余位全部清0, *********************************************************************
pengfoo 2013-01-03
  • 打赏
  • 举报
回复
拿ox01010101&ox01000000
zilaishuichina 2013-01-03
  • 打赏
  • 举报
回复
0x01010101 & 0x01000000 = 0x01000000
SKATE11 2013-01-02
  • 打赏
  • 举报
回复
如果是有符号数就左移29位 无符号数就左移30位
ForestDB 2013-01-02
  • 打赏
  • 举报
回复
与0x01000000相与
V_wo独尊 2013-01-02
  • 打赏
  • 举报
回复
右移31~ 走你~~~
vic_chang 2013-01-02
  • 打赏
  • 举报
回复
题目确实没有说清楚。 如果是考虑 数据中的1的个数之类的,确是采用移位运算即可
昵称很不好取 2013-01-02
  • 打赏
  • 举报
回复
什么意思? 可以考虑位运算,& 或者 ^
jimette 2013-01-02
  • 打赏
  • 举报
回复
直接输出

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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