Thanks Navin and Anand, Can you please refer me to a link where I can find more details around the representation of numbers, please.
Thanks again for big help. -- Best, Varun Narang. 2 is not equal to 3, not even for large values of 2. - Grabel's Law On Sat, Jun 30, 2012 at 3:30 PM, <bangpypers-requ...@python.org> wrote: > Send BangPypers mailing list submissions to > bangpypers@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/bangpypers > or, via email, send a message with subject or body 'help' to > bangpypers-requ...@python.org > > You can reach the person managing the list at > bangpypers-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of BangPypers digest..." > > > Today's Topics: > > 1. Need help understanding -9 >> 1 (Varun Narang) > 2. Re: Need help understanding -9 >> 1 (Navin Kabra) > 3. Re: Need help understanding -9 >> 1 (Anand Chitipothu) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Fri, 29 Jun 2012 15:40:30 +0530 > From: Varun Narang <varunar...@gmail.com> > To: bangpypers@python.org > Subject: [BangPypers] Need help understanding -9 >> 1 > Message-ID: > <cah_oqhhp2mfr_zdvpkhxhjtncvndjf3asaw9bphmvvphw-m...@mail.gmail.com > > > Content-Type: text/plain; charset=UTF-8 > > Hi all, > > I need some help understanding the right shift operation on -9. To my > understanding, it's represented as -0b1001, Now, if I shift it one place to > right, it should give me -0b0100, which is decimal equivalent of 4. but > running this on python console gives me -5. > > Please help me out here. > > Thanks, > Varun > > > ------------------------------ > > Message: 2 > Date: Fri, 29 Jun 2012 15:53:23 +0530 > From: Navin Kabra <navin.ka...@gmail.com> > To: Varun Narang <varunar...@gmail.com>, bangpypers@python.org > Subject: Re: [BangPypers] Need help understanding -9 >> 1 > Message-ID: <87k3yqpgbo....@smriti.com> > Content-Type: text/plain; charset=us-ascii > > Varun Narang <varunar...@gmail.com> writes: > > I need some help understanding the right shift operation on -9. To my > > understanding, it's represented as -0b1001, > > No. Integers are represented using 2s complement integers. So -9 will > actually be represented by 0xfffffff7. When you shift it to the right, a > '1' is shifted into the empty slot on the left. S you get: 0xfffffffb > which is -5. > > > > > ------------------------------ > > Message: 3 > Date: Fri, 29 Jun 2012 16:03:54 +0530 > From: Anand Chitipothu <anandol...@gmail.com> > To: Bangalore Python Users Group - India <bangpypers@python.org> > Subject: Re: [BangPypers] Need help understanding -9 >> 1 > Message-ID: > <cac7wxfxw-mf40zreqz0rb6ansb5mv5zqofjjaox-awctmzg...@mail.gmail.com > > > Content-Type: text/plain; charset=ISO-8859-1 > > On Fri, Jun 29, 2012 at 3:40 PM, Varun Narang <varunar...@gmail.com> > wrote: > > Hi all, > > > > I need some help understanding the right shift operation on -9. To my > > understanding, it's represented as -0b1001, Now, if I shift it one place > to > > right, it should give me -0b0100, which is decimal equivalent of 4. but > > running this on python console gives me -5. > > > > Please help me out here. > > -9 is represented internally as 0xFFFFFFF7. > > The last byte in binary is 11110111. When on rightshift, it becomes > 11111011. Which is 0xFFFFFFFB, hex representation of -5. > > Try this to see how -9 and -5 are represented internally: > > >>> import ctypes > >>> libc = ctypes.CDLL("libc.so.6") > >>> a = libc.printf("%x\n", -9) > fffffff7 > >>> a = libc.printf("%x\n", -5) > fffffffb > > This works only on linux. > > Read https://en.wikipedia.org/wiki/Signed_number_representations how > negative integers are represented internally. > > Anand > > > ------------------------------ > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > > > End of BangPypers Digest, Vol 58, Issue 27 > ****************************************** > _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers