bitwise shift?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • desktop

    #1

    bitwise shift?

    I have found a code example with this loop.

    for k in range(10, 25):
    n = 1 << k;


    I have never read Python before but is it correct that 1 get multiplied
    with the numbers 10,11,12,12,... ,25 assuming that 1 << k means "1 shift
    left by k" which is the same as multiplying with k.
  • Michael Hoffman

    #2
    Re: bitwise shift?

    desktop wrote:
    I have found a code example with this loop.
    >
    for k in range(10, 25):
    n = 1 << k;
    >
    >
    I have never read Python before but is it correct that 1 get multiplied
    with the numbers 10,11,12,12,... ,25
    No.
    assuming that 1 << k means "1 shift left by k"
    Yes.
    which is the same as multiplying with k.
    No.

    Try starting the Python interpreter and entering 1 << 10.
    --
    Michael Hoffman

    Comment

    • =?ISO-8859-1?Q?Thomas_Kr=FCger?=

      #3
      Re: bitwise shift?

      desktop schrieb:
      I have found a code example with this loop.
      >
      for k in range(10, 25):
      n = 1 << k;
      >
      >
      I have never read Python before but is it correct that 1 get multiplied
      with the numbers 10,11,12,12,... ,25 assuming that 1 << k means "1 shift
      left by k" which is the same as multiplying with k.

      1 << n
      is a more efficient replacement for
      2**n

      Thomas

      --
      sinature: http://nospam.nowire.org/signature_usenet.png

      Comment

      • Sherm Pendley

        #4
        Re: bitwise shift?

        desktop <fff@sss.comwri tes:
        for k in range(10, 25):
        n = 1 << k;
        >
        I have never read Python before but is it correct that 1 get
        multiplied with the numbers 10,11,12,12,... ,25 assuming that 1 << k
        means "1 shift left by k" which is the same as multiplying with k.
        Shift left is *not* the same as multiplying by k. It is the same as multi-
        plying by 2^k. That is, 1<<10 = 1024, 1<<11 = 2048, 1<<12 = 4096, etc.

        sherm--

        --
        Web Hosting by West Virginians, for West Virginians: http://wv-www.net
        Cocoa programming in Perl: http://camelbones.sourceforge.net

        Comment

        • Terry Reedy

          #5
          Re: bitwise shift?


          "desktop" <fff@sss.comwro te in message
          news:f0of5a$nn9 $1@news.net.uni-c.dk...
          |I have found a code example with this loop.
          |
          | for k in range(10, 25):
          | n = 1 << k;
          |
          |
          | I have never read Python before but is it correct ...

          One of the super-nice feature of Python is the interactive mode, also
          available with IDLE and other IDEs. that lets you explore the meaning of
          Python code faster than you can ask here. Use it and learn.
          >>for k in range(10, 25): print 1 << k
          1024
          2048
          4096
          8192
          16384
          32768
          65536
          131072
          262144
          524288
          1048576
          2097152
          4194304
          8388608
          16777216



          Comment

          • John Machin

            #6
            Re: bitwise shift?

            On 26/04/2007 7:10 AM, Sherm Pendley wrote:
            Shift left is *not* the same as multiplying by k. It is the same as multi-
            plying by 2^k.
            Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)

            Comment

            • Sherm Pendley

              #7
              Re: bitwise shift?

              John Machin <sjmachin@lexic on.netwrites:
              On 26/04/2007 7:10 AM, Sherm Pendley wrote:
              >
              >Shift left is *not* the same as multiplying by k. It is the same as multi-
              >plying by 2^k.
              >
              Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)
              Make that YMMV in VB. :-(

              Funny thing is, I haven't written in VB since '95 or thereabouts. Let that be
              a lesson for the youngsters - touch not the unclean beast called VB, lest its
              vileness stain thy code for all time!

              sherm--

              --
              Web Hosting by West Virginians, for West Virginians: http://wv-www.net
              Cocoa programming in Perl: http://camelbones.sourceforge.net

              Comment

              • Tobiah

                #8
                Re: bitwise shift?

                John Machin wrote:
                On 26/04/2007 7:10 AM, Sherm Pendley wrote:
                >
                >Shift left is *not* the same as multiplying by k. It is the same as
                >multi-
                >plying by 2^k.
                >
                Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)
                desktops:toby:g abc
                bc 1.06
                Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
                This is free software with ABSOLUTELY NO WARRANTY.
                For details type `warranty'.
                2^3
                8

                --
                Posted via a free Usenet account from http://www.teranews.com

                Comment

                • Tobiah

                  #9
                  Re: bitwise shift?

                  Tobiah wrote:
                  John Machin wrote:
                  >On 26/04/2007 7:10 AM, Sherm Pendley wrote:
                  >>
                  >>Shift left is *not* the same as multiplying by k. It is the same as
                  >>multi-
                  >>plying by 2^k.
                  >>
                  >Where I come from, ^ is the exclusive-or operator. Of course YMMV in
                  >WV :-)
                  >
                  desktops:toby:g abc
                  bc 1.06
                  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
                  This is free software with ABSOLUTELY NO WARRANTY.
                  For details type `warranty'.
                  2^3
                  8
                  >
                  Wow, thunderbird displayed this to me as a true exponent, even
                  though it is an ascii message. anyone else get this?


                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • mensanator@aol.com

                    #10
                    Re: bitwise shift?

                    On May 2, 2:24 pm, Tobiah <t...@tobiah.or gwrote:
                    John Machin wrote:
                    On 26/04/2007 7:10 AM, Sherm Pendley wrote:
                    >
                    Shift left is *not* the same as multiplying by k. It is the same as
                    multi-
                    plying by 2^k.
                    >
                    Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)
                    >
                    desktops:toby:g abc
                    bc 1.06
                    Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
                    This is free software with ABSOLUTELY NO WARRANTY.
                    For details type `warranty'.
                    2^3
                    8
                    OTOH,

                    IDLE 1.2c1
                    >>2^3
                    1

                    Guess which one is relevant to comp.lang.pytho n?

                    >
                    --
                    Posted via a free Usenet account fromhttp://www.teranews.co m

                    Comment

                    • Adonis Vargas

                      #11
                      Re: bitwise shift?

                      Tobiah wrote:
                      <snip>
                      Wow, thunderbird displayed this to me as a true exponent, even
                      though it is an ascii message. anyone else get this?
                      >
                      >
                      Yeah I can confirm Thunderbird 1.5.10 on Linux.

                      Adonis

                      Comment

                      Working...