Two's complement

I have learned two's complement in the computer organization course. At that time, I simply knew that to express a negative number, first find it's positive form, then flip every bit of it and finally add one on it. Although the specific steps are clear, I don't know the reason for it, in other words, I don't know why should I do this until I read an algorithm book which happens referred the two's complement.

The reason is this. For a n-bit long word, the all possible combination of numbers is 2n-1. Since we want to represent sign numbers, the range is (-2n-1 , 2n-1-1). Here’s a much simpler way to think about it: any number in this range is stored modulo 2n . Negative numbers −x therefore end up as 2n − x. Arithmetic operations like addition and subtraction can be performed directly in this format, ignoring any overflow bits that arise.

posted @ 2012-06-07 10:45  zdai  阅读(310)  评论(0编辑  收藏  举报