5

On my midterm, there was a question stating:

Given the decimal values, what is the minimum number of bits required to represent each number in Two's Complement form?

The values were: -26, -1, 10, -15, -4.

I did not get this question right whatsoever, and the solutions are quite baffling.

The only part I really understand is finding the range in which the value is located. For example, -15 would be within the range of [-2^5, 2^5), and -4 would be in the range from [-2^2, 2^2). What steps are needed from here in order to find how many bits were necessary?

I tried finding some pattern to solve it, but it only worked for the first two cases. Here's my attempt:

  1. First I found the range. -2^6 < -26 < 2^6

  2. Then I found the value for 2^6 = 32.

  3. Then I found the difference between the "closest" bound, and the value.

-26 - (-32) = 6

Again, this worked for the first two values by chance, and now I'm stumped as to find the actual relation between the number of bits required for an integer to be represented in Two's complement form, and the actual integer.

Thanks in advance!

Andrew T
  • 783
  • 4
  • 11
  • 20

1 Answers1

7

First off, you're off on your powers of 2. 32 = 25.

Anyway, I followed you through the first two steps. Your last step doesn't make sense.

  1. Find the power-of-two range that brackets the number. You want a power-of-two range of the form [-2N, 2N - 1]. So, for -26, that would be -25 &leq; -26 &leq; 25 - 1. That corresponds to -32 &leq; -26 &leq; 31.
  2. Number of bits for the 2s complement representation will then simply be N plus 1. The "plus 1" accounts for the sign bit. For -26, that's 5 + 1 = 6.

So, for each of the numbers you gave: -26, -1, 10, -15, -4.

  • -25 &leq; -26 &leq; 25 - 1 becomes -32 &leq; -26 &leq; 31, which gives 5 + 1 = 6.
  • -20 &leq; -1 &leq; 20 - 1 becomes -1 &leq; -1 &leq; 0, which gives 0 + 1 = 1.
  • -24 &leq; 10 &leq; 24 - 1 becomes -16 &leq; 10 &leq; 15, which gives 4 + 1 = 5.
  • -24 &leq; -15 &leq; 24 - 1 becomes -16 &leq; -15 &leq; 15, which gives 4 + 1 = 5.
  • -22 &leq; -4 &leq; 22 - 1 becomes -4 &leq; -4 &leq; 3, which gives 2 + 1 = 3.

Got it?

The -1 one is tricky...

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • Agreed -- seems like `ceil(log2(abs(_number_)))+1`, where the `+1` is to add an extra bit for the optional sign. – Jongware Dec 07 '13 at 00:33
  • 1
    @Jongware: Be careful. The range is asymmetric around the origin (the most-negative value has a larger magnitude than the most-positive value), but your suggestion to use `abs` is symmetric. – Joe Z Dec 07 '13 at 00:34
  • Yeah, my logic has gotten decently skewed since I started studying for finals so no worries there. But yes, it does make sense now! Essentially the task is to find what range of 2^x is needed that incorporates the value, and then you take x+1 to find the bits needed. Thanks! – Andrew T Dec 07 '13 at 00:35
  • @JoeZ: ah you are right. With bits, the negative part will always have one value more. No way to express that concisely :( – Jongware Dec 07 '13 at 00:37
  • Oh! I guess I only read the last phrase: "the -1 is tricky", and didn't realize it was already in your explanation :) Comment deleted. – mcleod_ideafix Dec 07 '13 at 02:09
  • @mcleod_ideafix : No worries. -1 is definitely tricky. Anyone who has accidentally declared a signed 1-bit bitfield in C has already discovered the variable that can only hold 0 or -1. – Joe Z Dec 07 '13 at 02:11