I know inversion operator will convert all 1 bits to 0 and vice versa. int x = 6 ; x = ~x; How is this valid statement?Could anyone explain for me. Thanks.

Hi suresh, You're probably thinking of the statement as an equality statement. It's actually an assignment statement, in which the inverse of x is assigned to x. Now, if the statement read: x == ~x then it would always be false (except when x = 0?) Hope this helps, Paul Villangca

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149

posted

0

Hi Jamal 1. 6(10) = 00000...0110(2) - 32 bits 2. ~6(10) = 11111...1001(2) = -7(10) 3. x = ~6 = -7 In line 2 what is -7(10) and how x = ~6 is valid again. Please explain. Sorry for not getting this bit manipulation into my mind very well.

Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056

posted

0

Now, if the statement read: x == ~x then it would always be false (except when x = 0?)

x==~x can never be true for any value of x. In particular, ~0 == -1.

Well... dunno: -x is ~x+1 so ~x is -(1+x); so x = ~x, can be written x = -(1+x). Therefore 2x = -1, giving x = -0.5 -Barry B.Sc Maths(Hons) [always wanted to write that ] [ August 14, 2002: Message edited by: Barry Gaunt ]