Most Negative 8-bit Value with Exactly Three Set Bits
Math & Bit ManipulationModel answerLast updated
Find the smallest two's-complement signed 8-bit integer whose binary representation contains exactly three 1 bits.
Most Negative 8-bit Value with Exactly Three Set Bits
A signed 8-bit integer is stored in two's-complement format. You are given the additional constraint that the bit pattern must contain exactly three 1 bits and five 0 bits.
Which of the following is the smallest (most negative) value achievable under this constraint?
- A.
-127 - B.
-32 - C.
-125 - D.
-3
Explain your reasoning, identify why each wrong answer is a plausible trap, and generalize the approach to an arbitrary bit-width n and exactly k set bits.
Hints
Hint 1
Write out the positional weights for each bit in two's-complement. Notice that only one bit position contributes a negative amount — which one, and what is its weight?
Hint 2
Once you've committed the sign bit, you still need to place two more 1 bits. Each remaining position adds a positive value, so to minimize the total you want to add as little as possible. Which two positions add the least?
Hint 3
Before finalizing your answer, count the 1 bits in each option. Some distractors represent valid two's-complement values that simply don't satisfy the 'exactly three 1 bits' constraint.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Answer: C. -125 (bit pattern 10000011)
Two's-Complement Weight Table
In an 8-bit two's-complement number, each bit position has a fixed weight. The only unusual one is the most-significant bit, which carries a negative weight:
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Weight | −128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
The value of any pattern b7 b6 … b0 is therefore:
value = −128·b7 + 64·b6 + 32·b5 + 16·b4 + 8·b3 + 4·b2 + 2·b1 + 1·b0
Greedy Placement of Three 1 Bits
The goal is to minimize this sum subject to b7 + b6 + … + b0 = 3.
Step 1 — Set the sign bit. Bit 7 is the only position that subtracts. Setting it contributes −128, which no combination of positive-weight bits can offset. If the sign bit is off, all three 1s land on positive-weight positions and the smallest achievable value is 1 + 2 + 4 = 7. Any negative number is smaller, so the sign bit must be on.
Step 2 — Minimize the remaining positive contribution. With the sign bit fixed, we need to place exactly two more 1s among the seven positive-weight positions (weights 1, 2, 4, 8, 16, 32, 64). To minimize their sum, pick the two smallest weights: bit 0 (+1) and bit 1 (+2).
Result: 10000011 = −128 + 2 + 1 = −125.
This is a tight greedy argument: the smallest sum of two distinct positive powers-of-two is 1 + 2 = 3, so −128 + 3 = −125 is the floor.
Why Each Distractor Is Wrong
A. −127 is 10000001 — only two 1 bits. It is more negative than −125, which makes it tempting, but it violates the exact-count constraint. This trap catches candidates who chase the most-negative value without checking the bit count.
B. −32 is 11100000 = −128 + 64 + 32. It has exactly three 1 bits, so it satisfies the constraint, but the two extra bits were placed at the highest positive weights (64 and 32) instead of the lowest. This is the largest negative value under the constraint, not the smallest.
D. −3 is 11111101 — seven 1 bits. This catches candidates who interpret "three ones" as "magnitude 3" or who are thinking in sign-magnitude rather than two's complement. In sign-magnitude 10000011 would indeed be −3, but in two's complement it is −125.
Common Mistakes
- Confusing sign-magnitude with two's complement. In sign-magnitude a few set bits means small magnitude. In two's complement, a single sign bit plus low-order bits produces a large negative magnitude.
- Assuming more
1bits means more negative.11111111is −1, nearly zero. Adding high-order1s after the sign bit pulls the value toward zero. - Skipping the bit-count check. Quickly verify popcount before committing to any candidate; two of the four options fail the constraint outright.
Generalization
For an n-bit word with exactly k set bits (1 ≤ k ≤ n), the minimum two's-complement value is:
minimum = −2^(n−1) + (2^(k−1) − 1)
Set the sign bit, then fill the k − 1 lowest positions. For n = 8, k = 3: −128 + (4 − 1) = −128 + 3 = −125. Edge cases: k = 1 gives just the sign bit (−2^(n−1)); k = n gives all ones, which is always −1; k = 0 gives 0 (the formula doesn't apply).
Python Verification
def to_signed(pattern: int, n: int) -> int:
"""Reinterpret an n-bit unsigned integer as two's complement."""
sign_bit = 1 << (n - 1)
return pattern - (sign_bit << 1) if pattern & sign_bit else pattern
def brute_force_min(n: int, k: int) -> int:
"""Exhaustive search: check all 2^n patterns for popcount == k."""
results = [
to_signed(p, n)
for p in range(1 << n)
if bin(p).count("1") == k
]
if not results:
raise ValueError(f"No {n}-bit pattern has exactly {k} ones")
return min(results)
def closed_form_min(n: int, k: int) -> int:
"""Sign bit plus the k-1 lowest-weight bits."""
if k == 0:
return 0
if not 1 <= k <= n:
raise ValueError("k must be between 1 and n inclusive")
return -(1 << (n - 1)) + ((1 << (k - 1)) - 1)
if __name__ == "__main__":
# Verify the specific question
result = closed_form_min(8, 3)
assert result == brute_force_min(8, 3) == -125
print(f"Answer: {result}, bit pattern: {result & 0xFF:08b}")
# -125, 10000011
# Cross-validate formula against brute force for small widths
for n in range(1, 12):
for k in range(0, n + 1):
assert closed_form_min(n, k) == brute_force_min(n, k), \
f"Mismatch at n={n}, k={k}"
print("All checks passed.")
Time complexity: The closed-form answer is O(1). The brute-force scan is O(2ⁿ · n), useful only for verification on small bit-widths.
What a Strong Answer Demonstrates
A strong candidate writes out the weight table immediately, states the greedy strategy in one sentence ("set the sign bit, then use the two smallest positive weights"), and explicitly counts the bits in every option before committing. They can also state the general formula and name the k = 0 and k = n boundary cases without prompting.