如何快速确定整数中的位数
How to quickly determine how many digits in an int?
我知道我可以做到:
n = floor(log10(i)) + 1;
或者我可以做一个快速循环:
while(i) {
n++;
i/=10;
}
有没有比复杂的数学运算或循环更好的方法来实现目标?例如:如果 i = 1234,则 n = 4。
我知道的最短方法(不是计算上的,只是在键入方面(是调用snprintf(3)
:
int n = snprintf(NULL, 0, "%d", i);
将其
转换为字符串(itoa(并计算字符数?(虽然可能不是最好的性能(
相关文章:
- 有没有一种优雅而快速的方法来测试整数中的 1 位是否位于连续区域
- 快速扫描标准以获取整数不起作用
- C++一个具有 2 个参数(___ _____,整数长度)的函数中的快速排序
- 快速方法可以将整数乘以适当的分数,而无需浮点或溢出
- 如何在 9 位整数中的所有"1"之后快速设置 64 位
- 什么是快速整数?定义整数的速度
- 快速实现大整数计数器(在C/C 中)
- 超快速搜索整数子字符串的方法
- 从 2+ 个整数(快速)创建哈希/种子值
- 从集合中随机给定整数 0、1 和 2 时获取整数 0、1 和 2 的快速方法
- 快速整数矩阵乘法与位摆动技巧
- 位字段可以用作"穷人"的快速整数类型吗?
- 如何将 6 字节无符号整数快速复制到内存区域
- c++快速排序算法中500000排序整数数组的Seg错误
- 整数部分破解快速平方根反比
- 整数的快速排序算法
- 如何快速确定整数中的位数
- 快速,快速随机整数生成器
- 无符号整数的快速无分支最大值
- 整数的快速集合并集