2

I am adding a series of 8196 64-bit unsigned integers, and I need the running-total to "roll-over" back to zero and continue counting from there... just as a "normal" programing language would do at the relevent INT_MAX ceiling.

As the test script shows, adding 1 to an boundary value (FF, FFFF, etc) just keeps on increasing the total. A feature, no doubt, but I'd like to limit it to 64-bits for this particular instance..

Is there some way to limit bc in this?

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
Peter.O
  • 6,696
  • 4
  • 30
  • 37

1 Answers1

1

This is called a modulo and you can read https://superuser.com/questions/31445/gnu-bc-modulo-with-scale-other-than-0 here about modulo and bc.

Community
  • 1
  • 1
chx
  • 11,270
  • 7
  • 55
  • 129
  • Ah.. thanks... I was focused on, and expected it to be, a 'bc' setting, like `BC_LINE_LENGTH=nnn` or `scale` or some such thing...and I've never quite thought of a modulo in this way... ie. that it effectively truncates the high-order end.. I've only ever thought of it as the remainder of an "ever-present" quotient... Okay,neat... so I just need to do a modulo after each addition... :) – Peter.O May 15 '11 at 11:52