Question

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!

Was it helpful?

Solution

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 ≤ -26 ≤ 25 - 1. That corresponds to -32 ≤ -26 ≤ 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 ≤ -26 ≤ 25 - 1 becomes -32 ≤ -26 ≤ 31, which gives 5 + 1 = 6.
  • -20 ≤ -1 ≤ 20 - 1 becomes -1 ≤ -1 ≤ 0, which gives 0 + 1 = 1.
  • -24 ≤ 10 ≤ 24 - 1 becomes -16 ≤ 10 ≤ 15, which gives 4 + 1 = 5.
  • -24 ≤ -15 ≤ 24 - 1 becomes -16 ≤ -15 ≤ 15, which gives 4 + 1 = 5.
  • -22 ≤ -4 ≤ 22 - 1 becomes -4 ≤ -4 ≤ 3, which gives 2 + 1 = 3.

Got it?

The -1 one is tricky...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top