Selecting the necessary number type

With max positive M,

  • unsigned : ceil(log2(M + 1)) bits needed
  • signed: ceil(log2(M + 1)) + 1 bits needed