0

The literature mentions several formulae for determining the number of bits required to store the binary expression of a given integer n if n > 0.

Example: the number of binary digits required to store the integer 13 is 4: $$\text{Binary Expression of }13_{10}\text{: 1101}_{2}$$

Concrete Mathematics suggests two ways to get the number of bits required to store a given positive integer with a function B(n) such that:

$$B(n) = \lfloor log_2(n)\rfloor+ 1$$

or $$B(n) = \lceil log_2(n + 1)\rceil$$

For all integers greater than zero both functions give the correct number of bits; however, the first function using a floor fails to render an integer result when n = 0 and yields undefined, while the second function using a ceiling yields 0.

Graham, Knuth, and Patashnik note for the latter function that it holds for zero "if we're willing to say that it takes zero bits to write n = 0 in binary."

This post is concerned with the case that we ARE NOT willing to say that it takes zero bits to write n = 0 in binary, OR, more correctly, that we MUST require at least one binary digit to store a 0 or 1 in a theoretical information storage system. Thus the domain I'm concerned with is $n\geq0$.

One solution uses a piecewise function: $$B(n) := \begin{cases}n = 0 & 1\\ n > 0 & \lceil log_2(n + 1)\rceil \\ \end{cases}$$

I implemented this in Excel when iterating over a set of positive integers (with zeroes) to find the minimum bits required to store each while researching a certain bit-packing encoding: =IF(A1=0,1,CEILING(LOG(A1+1,2),1)) or =IF(A1>0,CEILING(LOG(A1+1,2),1),1)

Is there a non-piecewise function that can accomplish the same result?

Examples (n, B(n)):

(0, 1) (1, 1) (2, 2) (3, 2) (4, 3) (5, 3) (6, 3) (7, 3) (8, 4) ...

Note: This question is similar but distinct to the following MathExchange questions: 1416606, 160295, 927468, 3336664

  • 1
    "Non-piecewise" is a sort of arbitrary condition if the rest of mathematics is available to you. For instance you could take your previous $B(n) = \lceil \log_2 (n+1) \rceil$ and add $1-\lceil n/(n+1) \rceil$. But why is one any better than the other? – Nate Eldredge Aug 14 '21 at 21:51
  • Your question is not very clear. I think that when you say "piecewise function" you mean a function that is defined by cases. Why do you care about the need for a conditional (the "IF" in your Excel formula) that represents the definition by cases? – Rob Arthan Aug 14 '21 at 21:56
  • @NateEldredge Yep, it is indeed arbitrary! That's why I'm asking about whether a function exists that doesn't involve conditional logic. As for ⌊log2(n+2)⌋: no, this one fails on (4,2) (5,2) (8,3) and so on. – Tim Cotten Aug 14 '21 at 22:02
  • @RobArthan You're correct that it's being defined by cases; and it's less that I "care" and more than I want to "know", ha! – Tim Cotten Aug 14 '21 at 22:03
  • 4
    Okay, so then $B(n) = \lceil \log_2(n+1) \rceil + 1 - \lceil n/(n+1) \rceil$. If that makes you happy then great. But the point is that there's no consistent way to distinguish "conditional logic" from the rest of math. – Nate Eldredge Aug 14 '21 at 22:05
  • Note that if actually implemented in a spreadsheet, this may give incorrect results for large inputs, due to the finite precision of floating-point arithmetic. The conditional is actually better in that regard. – Nate Eldredge Aug 14 '21 at 22:07
  • 2
    Tim, I too used to think piecewise functions were some kind of mathematical fudge, an impure function, so to speak. But this kind of thinking is pointless - maths is just maths, definitions rule supreme, and a function by any definition is just a function, even if there is no simple plug-and-play formula to compute some functions – FShrike Aug 14 '21 at 22:08
  • @NateEldredge Brilliant! And yes, I understand you and FShrike's point about piecewise functions not being a "mathematical fudge" and that "maths is just maths". The reason I asked wasn't because of judgment, but to see which would resolve to a cheaper set of instructions in machine language (conditional branch or any additional evaluations). Your formula works perfectly for testing that, and thanks! Please submit it as an answer with "Yes". – Tim Cotten Aug 14 '21 at 22:13
  • Fair point, but the power of the vocabulary provided by Excel functions isn't of any particular mathematical interest. If (as suggested by your most recent comment) your concern is with the performance of your Excel spreadsheet, then I think MSE is the wrong forum for your question (and I think you will find that the minutiae of how many conditionals you use in your formulas is completely irrelevant to the performance). How do you think the floor and ceiling functions are implemented? – Rob Arthan Aug 14 '21 at 22:19
  • @RobArthan Ah, I see the confusion. Yes, I referenced Excel in the post because I was sanity checking the values. The actual project is using machine language, in this case, x86 Assembly, to build the function that finds the bitrate I need to encode a given integer at. Nate's answer is something I can recreate in an instruction set and then compare the size and performance against the conditional version - so this was a very valuable contribution from my perspective! :) – Tim Cotten Aug 14 '21 at 22:25
  • OK. Sorry about the misunderstanding. This isn't an MSE thing, but I'd bet you a beer that on modern hardware (with lots of lookahead), the extra conditionals don't make any difference. – Rob Arthan Aug 14 '21 at 22:32

1 Answers1

3

For instance, $B(n) = \lceil \log_2(n+1) \rceil + 1 - \lceil n/(n+1) \rceil$, taking advantage of the fact that $n/(n+1)$ is zero when $n=0$ and between 0 and 1 otherwise.

I think it's unlikely that there is any practical situation where this is a superior alternative.

Nate Eldredge
  • 97,710