Thursday, 15 September 2011

math - Why we need to add 1 while doing 2's complement -



math - Why we need to add 1 while doing 2's complement -

the 2's complement of number represented n bits 2^n-number. example: if number 7 (0111) , i'm representing using 4 bits then, 2's complement of (2^n-number) i.e. (2^4 -7)=9(1001)

7==> 0111 1's compliment of 7==> 1000 1000 + 1 ------------- 1001 =====> (9)

while calculating 2's complement of number, next steps: 1. one's complement of number 2. add together 1 result of step 1.

i understand need one's complement of number because doing negation operation. why add together 1?

this might silly question i'm having hard time understanding logic. explain above illustration (for number 7), one's complement , -7 , add together +1, -7+1=-6, still getting right reply i.e. +9

your error in "we one's compliment , -7". see why wrong, take one's complement of 7 , add together 7 it. if it's -7, should 0 because -7 + 7 = 0. won't.

the one's complement of 7 1000. add together 7 that, , 1111. not zero. need add together 1 more zero!

the negative of number number need add together zero.

if add together 1 ...11111, zero. -1 represented 1 bits.

if add together number, x, 1's complement ~x, 1 bits.

thus: ~x + x = -1

add 1 both sides: ~x + x + 1 = 0

subtract x both sides: ~x + 1 = -x

math bit-manipulation twos-complement

No comments:

Post a Comment