Skip to content
UniCircle
EasyNumericalDigital electronics

Binary, hexadecimal and 8-bit two's complement conversion

Question ECE-102

Convert decimal 45 to binary and hexadecimal. Then write −45 as an 8-bit two's complement number in binary and hex, and verify your answer.

Show hints

Two's complement = invert all bits, then add 1.

Show the explanation

Worked answer.

Step 1: 45 in binary. Break 45 into powers of two: 45 = 32 + 8 + 4 + 1.
In 8 bits: 0010 1101.

Step 2: 45 in hex. Group the bits in nibbles: 0010 = 2, 1101 = D. So 45 = 0x2D. Check: 2 × 16 + 13 = 45.

Step 3: −45 in two's complement.

  • Start with +45: 0010 1101
  • Invert every bit (one's complement): 1101 0010
  • Add 1: 1101 0011

In hex this is 0xD3.

Step 4: Verify.

  • Method 1: In 8-bit two's complement, the MSB has weight −128. 1101 0011 = −128 + 64 + 16 + 2 + 1 = −45. Correct.
  • Method 2: The two's complement of N in 8 bits equals 256 − N. 256 − 45 = 211, and 211 = 0xD3. Correct.
  • Method 3: Adding +45 and −45: 0010 1101 + 1101 0011 = 1 0000 0000. The carry out of bit 7 is discarded, leaving 0000 0000. Correct.

Range reminder. An 8-bit two's complement number covers −128 to +127. The MSB is the sign bit, and there is only one representation of zero, which is why two's complement replaced sign-magnitude in hardware.

Embedded relevance. In C, if you store 0xD3 in an int8_t it reads as −45, but in a uint8_t it reads as 211. Many sensor bugs come from reading a signed register into an unsigned variable.

Common mistake. Forgetting the "+1" step and stopping at the one's complement (1101 0010 = −46 in two's complement).

Likely follow-up. "How do you detect overflow when adding two signed numbers?" Overflow occurs when two operands of the same sign give a result of the opposite sign, which is equivalent to the carry into the MSB differing from the carry out of it.

Original UniCircle question. Last reviewed 28 Oct 2026.