1

How would you get the 2's complement of a 4 bit number without explicitly using 0001 in the formula (using power - 1111 - or ground - 0000)?

I tried using a splitter but reconstructing the separate 1 bit numbers back to a 4 bit number did not work.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • Since 2's complement lies in a circle, start walking at 0 to opposite ways, if any of the walker finds your number, that means the other one is standing at the complement of it. – Seçkin Savaşçı Jan 28 '13 at 00:18
  • This question is unclear - primarily, the the goal is a *logic circuit* (in which case I suspect there are better stacks) or about *using assembly*? –  Jan 28 '13 at 00:52

2 Answers2

1

You could put the 4 bits into the upper 4 bits of a byte, negate the byte, then look at the upper four bits.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
xpda
  • 15,585
  • 8
  • 51
  • 82
1

To calculate two's complement with logic, one typically uses a chain of half adders:

(sum,c_out) = HA(a,b) ==  
 c_out := a & b,
 sum := a ^ b;  

The first HA is actually a tautology: bit_0 == a_0, c_1 == 1, and can be optimized out, if so wanted. Also the last carry out c_4 is rejected.

(bit_0,c_1)   := HA(not a_0, c_0 = 1)
(bit_1,c_2)   := HA(not a_1, c_1)
(bit_2,c_3)   := HA(not a_2, c_2)
(bit_3,[c_4]) := HA(not a_3, c_3)

With assembler one can use the fact that twos_comp(i) for n bit number == 2^n - i, for i!=0.

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57