|
@ -296,7 +296,31 @@ int logicalNeg(int x) { |
|
|
* Rating: 4 |
|
|
* Rating: 4 |
|
|
*/ |
|
|
*/ |
|
|
int howManyBits(int x) { |
|
|
int howManyBits(int x) { |
|
|
return 0; |
|
|
|
|
|
|
|
|
x = ((~(x>>31))&x)|(x>>31&~x);//左边为如果为正数,则保留;右边为如果为负数,则取反 |
|
|
|
|
|
//先从最大开始查找 |
|
|
|
|
|
int tf = !!(x>>16); //如果小于等于16位,则为0,否则为1 |
|
|
|
|
|
int b16 = tf << 4; //长度超过16位,记录值b16为16,否则为0,代表至少16位 |
|
|
|
|
|
x = x>>b16; //右移动b16位,超过16位则探讨16位以上的,少于16位则探讨16位以下的 |
|
|
|
|
|
|
|
|
|
|
|
tf = !!(x>>8); //开始探讨剩余部位是否超过8位,超过看超过多少,没超过看余下多少 |
|
|
|
|
|
int b8 = tf << 3;//长度超过8位,记录值b8为8,否则为0,代表再多至少8位 |
|
|
|
|
|
x = x>>b8; |
|
|
|
|
|
|
|
|
|
|
|
tf = !!(x>>4); |
|
|
|
|
|
int b4 = tf << 2 ; //8找4,长度超过4位,记录值b4为4,否则为0,代表再多至少4位 |
|
|
|
|
|
x = x>>b4; |
|
|
|
|
|
|
|
|
|
|
|
tf = !!(x>>2); |
|
|
|
|
|
int b2 = tf << 1; //4找2,长度超过2位,记录值b2为2,否则为0,代表再多至少2位 |
|
|
|
|
|
x = x>>b2; |
|
|
|
|
|
|
|
|
|
|
|
tf = !!(x>>1); //2找1,长度超过1位,记录值b1为1,否则为0,代表再多至少1位 |
|
|
|
|
|
int b1 = tf << 0; |
|
|
|
|
|
x = x>>b1; |
|
|
|
|
|
|
|
|
|
|
|
int b0 = x;//截断到只剩单个数字,这个数字是1就再多一位,是0就不会增加 |
|
|
|
|
|
|
|
|
|
|
|
return b0+b1+b2+b4+b8+b16+1; //正数多一位补码最高位,负数因为取反未+1,理应再加1,故两个输出表达式一致 |
|
|
} |
|
|
} |
|
|
//float |
|
|
//float |
|
|
/* |
|
|
/* |
|
|