将数字表示为一组连续位的最快方法是什么? 数字范围:1-32,输出存储在无符号32位变量中。
输入:1
输出:1000000000000000000000000000000000
输入:5
输出:11111000000000000000000000000000
输入:16
输出:11111111111111110000000000000000
输入:31
输出:11111111111111111111111111111110
我使用的是C语言,但是如果你能在汇编中给出一个更快的例子,那就很受欢迎了。
我目前的代码是:
uint get_bitmap_left(uint num) {
if (num== 1) return(0x80000000);
if (num== 2) return(0xC0000000);
if (num== 3) return(0xE0000000);
if (num== 4) return(0xF0000000);
if (num== 5) return(0xF8000000);
if (num== 6) return(0xFC000000);
if (num== 7) return(0xFE000000);
if (num== 8) return(0xFF000000);
if (num== 9) return(0xFF800000);
if (num==10) return(0xFFC00000);
if (num==11) return(0xFFE00000);
if (num==12) return(0xFFF00000);
if (num==13) return(0xFFF80000);
if (num==14) return(0xFFFC0000);
if (num==15) return(0xFFFE0000);
if (num==16) return(0xFFFF0000);
if (num==17) return(0xFFFF8000);
if (num==18) return(0xFFFFC000);
if (num==19) return(0xFFFFE000);
if (num==20) return(0xFFFFF000);
if (num==21) return(0xFFFFF800);
if (num==22) return(0xFFFFFC00);
if (num==23) return(0xFFFFFE00);
if (num==24) return(0xFFFFFF00);
if (num==25) return(0xFFFFFF80);
if (num==26) return(0xFFFFFFC0);
if (num==27) return(0xFFFFFFE0);
if (num==28) return(0xFFFFFFF0);
if (num==29) return(0xFFFFFFF8);
if (num==30) return(0xFFFFFFFC);
if (num==31) return(0xFFFFFFFE);
if (num==32) return(0xFFFFFFFF);
returrn(0);
}
答案 0 :(得分:2)
return 0xFFFFFFFF << (32 - num);