value power two
1 /** 2 *topPower2 use to set unsigned int to power of two 3 *@param value input value 4 *@return return the value's of the value,if input 3 return 4, if input 4 return 4 5 */ 6 size_t topPower2(size_t value){ 7 if((value & (value-1)) == 0){ 8 return value; 9 } 10 unsigned long max_ulong = (unsigned long)((unsigned long)~0); 11 unsigned long mid = ~(max_ulong&(max_ulong>>1)); 12 while((mid & value) == 0){ 13 mid = mid>>1; 14 } 15 return mid<<1; 16 }
书到用时方恨少,是非经过不知难。
博观而约取,厚积而薄发。@karllen 每天进步一点点。