KatInterview

Most Negative 8-bit Value with Exactly Three Set Bits

Math & Bit ManipulationModel answer
MediumModel answerMath & Bit Manipulationbit-manipulationtwo's-complementsigned-integers

Last 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.