算法计算出多少字节,须持有一个int字节、算法、计算出、int

2023-09-11 06:01:34 作者:蝶舞飛揚

抱歉愚蠢的问题,而是如何,我会去搞清楚,数学或使用C ++,这将有多少字节需要存储一个整数。

sorry for the stupid question, but how would I go about figuring out, mathematically or using c++, how many bytes it would take to store an integer.

推荐答案

您可以找到2,它比你的一些大的第一动力,并分割功率8,然后将该数字四舍五入到最接近的整数。因此,对于1000,2的幂是1024或2 ^ 10; 10分8拿到1.25,并四舍五入到2。你需要两个字节来容纳1000!

You could find the first power of 2 that's larger than your number, and divide that power by 8, then round the number up to the nearest integer. So for 1000, the power of 2 is 1024 or 2^10; divide 10 by 8 to get 1.25, and round up to 2. You need two bytes to hold 1000!