如何使 BC 中的十六进制加法在 8 字节限制 (uint64) 处溢出



我正在添加一系列 8196 个 64 位无符号整数,我需要运行总计"翻转"回零并从那里继续计数......就像"正常"的编程语言在相关的天花板INT_MAX所做的那样。

如测试脚本所示,在边界值(FF、FFFF 等)上加 1 只会不断增加总数。毫无疑问,这是一个功能,但我想在这个特定实例中将其限制为 64 位。

有没有办法限制这方面的bc

unset f 
for ((i=0; i<8; i++)); do 
  f=${f}FF; echo -ne "$((${#f}/2)) bytes + 1      " 
  echo 'ibase=16; obase=10; ('$f'+1)' |bc 
done
echo "I want 8th+1 to = 0000000000000000"
# output
#
# 1 bytes + 1      100
# 2 bytes + 1      10000
# 3 bytes + 1      1000000
# 4 bytes + 1      100000000
# 5 bytes + 1      10000000000
# 6 bytes + 1      1000000000000
# 7 bytes + 1      100000000000000
# 8 bytes + 1      10000000000000000
# I want 8th+1 to = 0000000000000000

这称为模数,您可以在此处阅读有关模数和 bc https://superuser.com/questions/31445/gnu-bc-modulo-with-scale-other-than-0。

最新更新