[BangPypers] BangPypers Digest, Vol 58, Issue 27

Varun Narang varunarang at gmail.com
Sat Jun 30 17:19:27 CEST 2012


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-request at python.org> wrote:

> Send BangPypers mailing list submissions to
>        bangpypers at 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-request at python.org
>
> You can reach the person managing the list at
>        bangpypers-owner at 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 <varunarang at gmail.com>
> To: bangpypers at python.org
> Subject: [BangPypers] Need help understanding -9 >> 1
> Message-ID:
>        <CAH_oQHHp2mFR_ZdvPKHxhjtNCVndJf3ASAw9BpHMVvPHW-MgvQ at 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.kabra at gmail.com>
> To: Varun Narang <varunarang at gmail.com>, bangpypers at python.org
> Subject: Re: [BangPypers] Need help understanding -9 >> 1
> Message-ID: <87k3yqpgbo.fsf at smriti.com>
> Content-Type: text/plain; charset=us-ascii
>
> Varun Narang <varunarang at 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 <anandology at gmail.com>
> To: Bangalore Python Users Group - India <bangpypers at python.org>
> Subject: Re: [BangPypers] Need help understanding -9 >> 1
> Message-ID:
>        <CAC7wXFxW-mF40ZreQZ0rb6aNsB5MV5ZQOFjJaOx-AWCTMzGmpw at mail.gmail.com
> >
> Content-Type: text/plain; charset=ISO-8859-1
>
> On Fri, Jun 29, 2012 at 3:40 PM, Varun Narang <varunarang at 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 at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>
> End of BangPypers Digest, Vol 58, Issue 27
> ******************************************
>


More information about the BangPypers mailing list