算法计算出多少字节,须持有一个int [英] algorithm to figure out how many bytes are required to hold an int

查看:201
本文介绍了算法计算出多少字节,须持有一个int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉愚蠢的问题,而是如何,我会去搞清楚,数学或使用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!

这篇关于算法计算出多少字节,须持有一个int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆