### C language rounds a number up to the power of 2 (a strange trick)

C language rounds a number up to the power of 2 (a strange trick)

``````uint32_t up_pow2(uint32_t n) {
--n;
for (int i = 0; i < 5; i++) {
n |= n >> (1 << i);
}
return ++n;
}

or

uint32_t up_pow2(uint32_t n) {
--n;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return ++n;
}``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.6189388,"math_prob":0.9949421,"size":442,"snap":"2022-40-2023-06","text_gpt3_token_len":159,"char_repetition_ratio":0.15981735,"word_repetition_ratio":0.39215687,"special_character_ratio":0.45927602,"punctuation_ratio":0.121212125,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9750506,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-30T22:04:39Z\",\"WARC-Record-ID\":\"<urn:uuid:c6964330-7b03-49c7-b516-05b85f797235>\",\"Content-Length\":\"107720\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:558c03e1-daf2-4f21-aa0e-f311e331e3f2>\",\"WARC-Concurrent-To\":\"<urn:uuid:fb235eb5-89ea-4e76-832c-6e72e625fe2f>\",\"WARC-IP-Address\":\"104.21.65.183\",\"WARC-Target-URI\":\"https://code.tushushare.com/2022/05/c-language-rounds-number-up-to-power-of.html\",\"WARC-Payload-Digest\":\"sha1:VWHA7VVMJRL7WVSSIJWZOY3JSWUTSROT\",\"WARC-Block-Digest\":\"sha1:X3FC47QNKRW6RCUWWH4GD4OENJS5XJIF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335504.37_warc_CC-MAIN-20220930212504-20221001002504-00535.warc.gz\"}"}