Exception:
Caused by: java.util.IllegalFormatConversionException: f != java.lang.Long
Cause:
private void updateSizeInfo() {
long size = FileHelper.getDirSize(Objects.requireNonNull(getContext()).getExternalFilesDir(null));
LogUtils.d(size);
String strSize = size > 1 << 20 ? PalmUtils.formatString(R.string.cache_size_mb, size >> 20)
: PalmUtils.formatString(R.string.cache_size_kb, size >> 10);
getBinding().amiCache.setFooter(strSize);
}
I use the code above to calculate the size of memory used, notice that I used the >> operation, here i want to write down something about the difference between >> and /.
Of course that you can use size * 1.0 / 1024 / 1024 to transfer bytes to MB, the goodness is that we can get the float result, the size >> 20 is more efficient but can only get the long result.
That is the problem, if I used >> and %1.f then i will get the exception.
本文探讨了在计算内存使用大小时采用位运算右移(>>)相较于除法(/)的优势与局限性。通过实例展示了如何利用位运算进行高效的数据单位转换,并讨论了在格式化输出时需要注意的问题,特别是当涉及浮点数显示时可能引发的异常。
2099

被折叠的 条评论
为什么被折叠?



