Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!hao!oddjob!gargoyle!ihnp4!ihlpg!tainter From: tainter@ihlpg.ATT.COM (Tainter) Newsgroups: comp.lang.c Subject: Re: Simple question about: ~ Message-ID: <4719@ihlpg.ATT.COM> Date: 29 Jan 88 16:38:56 GMT References: <1620006@hpcilzb.HP.COM> <2550041@hpisod2.HP.COM> Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 21 In article <2550041@hpisod2.HP.COM>, decot@hpisod2.HP.COM (Dave Decot) writes: > > Could someone please explain why the following statements both give the > > same answer? > > short int x, y = 12; > > x = -y -1; > > x = ~y; > The one's complement means flip all bits. (~ x) > The two's complement means flip all bits and add one. (- x) In fact, given the definitions it should be obvious that (~x) == (-x - 1) A derivation for those having long since seen their intro algebra classes :-) (-x - 1) :=: ((~x+1) - 1) (-x - 1) :=: (~x + (1 - 1)) (-x - 1) :=: (~x) > Dave Decot --j.a.tainter