I started the day windering if one might do subbbbbtraction on a quantum
computer, using two's complement. Turns out there already is a built subtraction
of tis kind, in the comparator() function.
So what is two's complement. With binary numbers, one subtracts one number
form the other by flipping the elements of the lesse number, adding 1 to it. And
adding the two numbers. the result - from which the last carry is discarded - is the
answer.
8 - 5 converts to 1000 minus 0101
Flipping 0101 gives 1010
Adding 1 gives 1011
Adding the two numbers is 1000 plus 1011, and discarding the most significant carry =
1011 + 1000 = 0011
And that is 3, the correct answer...
Below, the comaparator function in action:
No comments:
Post a Comment