本文介绍了如何仅使用宏来查找掩码的最低位位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何仅使用宏找到掩码的最低位位置? 我想在编译时完成所有操作。这意味着,宏中不能有 控制语句,如if,while,for等。 注意宏只占一个( 1)论证,面具。 例如: //面具最低位位置 0111 0000 4 0001 0000 4 0001 1000 3 0011 1111 0 谢谢。 解决方案 #define LOWEST_1_BIT(MASK)\ (((MASK)& 0001)?0:/ *位0设置?* / ((MASK)& 0002)?1:/ *第1位设置?* / ((MASK)& 0004)?2:/ *第2位设置?* / ((MASK)& 0010 )?3:/ *第3位设置?* / ((MASK)& 0020)?4:/ *第4位设置?* / / * ... * / -1)/ *没有设置位* / - char a [ ] =" \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b [] = {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6 ,0x1f6},* p = b,x,i = 24; for(; p + =!* p; * p / = 4)switch(x = * p& 3)case 0:{返回0; for(p - ; i - ; i - )case 2:{i ++; if(1)break; else default:continue; if(0)case 1:putchar (a [i& 15]); break;}}} 您可以按顺序使用很多?:运算符,但 你需要N个运算符,其中N是比特数。 另外你可以使用lg N宏,比如(关闭袖口) #define LOWESTBITPOS2(x)(x& 0x1 ?0:1) #define LOWESTBITPOS4(x)(x& 0x3?LOWESTBITPOS2(x):2 + LOWESTBITPOS2(x>> 2)) #define LOWESTBITPOS8(x)(x& 0xF?LOWESTBITPOS4(x):4 + LOWESTBITPOS4(x>> 4)) #define LOWESTBITPOS16(x)(x& 0xFF?LOWESTBITPOS8 (x):8 + LOWESTBITPOS8(x>> 8)) #define LOWESTBITPOS32(x)(x& 0xFFFF?LOWESTBITPOS16( x):16 + LOWESTBITPOS16(x>> 16)) #define LOWESTBITPOS(x)LOWESTBITPOS32(x) 如果这样有效,那我就写了。如果没有,那么一些不合群的 邪恶的骗子会侮辱我。无论如何,请注意假设32 位最大。 How do I find the lowest bit position of a mask using only macros? I want to do everything in compile time. That mean, there cannot becontrol statements such as if, while, for, etc. in the macro. Note that the macro takes only one (1) argument, the mask. Examples: // Mask Lowest bit position0111 0000 40001 0000 40001 1000 30011 1111 0 Thanks. 解决方案 #define LOWEST_1_BIT(MASK) \(((MASK) & 0001) ? 0 : /* bit 0 is set? */((MASK) & 0002) ? 1 : /* bit 1 is set? */((MASK) & 0004) ? 2 : /* bit 2 is set? */((MASK) & 0010) ? 3 : /* bit 3 is set? */((MASK) & 0020) ? 4 : /* bit 4 is set? *//* ... */-1) /* no bits are set */ --char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}} You could do that with a lot of ?: operators in sequence, butyou''d need N operators where N is the number of bits. Alternative you could use lg N macros, like (off the cuff)#define LOWESTBITPOS2( x ) (x & 0x1? 0 : 1)#define LOWESTBITPOS4( x ) (x & 0x3? LOWESTBITPOS2( x ) : 2+LOWESTBITPOS2( x >> 2 ))#define LOWESTBITPOS8( x ) (x & 0xF? LOWESTBITPOS4( x ) : 4+LOWESTBITPOS4( x >> 4 ))#define LOWESTBITPOS16( x ) (x & 0xFF? LOWESTBITPOS8( x ) : 8+LOWESTBITPOS8( x >> 8 ))#define LOWESTBITPOS32( x ) (x & 0xFFFF? LOWESTBITPOS16( x ) : 16+LOWESTBITPOS16( x >> 16 )) #define LOWESTBITPOS( x ) LOWESTBITPOS32( x )If this works, then I wrote it. If not, then some unscroupolousevil charlatan impersionated me. Anyway, note the assumption of 32bits maximum. 这篇关于如何仅使用宏来查找掩码的最低位位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 21:25