bit shifting question

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

    bit shifting question

    Hi,
    My background is c/c++ and java. I'm learning python at this point.

    My question is does python share java's peculiar mode of bit shifting, or
    does python adhere closer to c's bit shifting?

    in java there are 3 kinds of bit shifts:
    << (shift left)[color=blue][color=green]
    >> (preserve the sign bit as we move right )[color=darkred]
    >>> (0 filled on the left as we move right)[/color][/color][/color]

    In C, the behavior is
    << (shift left)[color=blue][color=green]
    >> (shift right , 0 filled (like java's >>>'))[/color][/color]

    I tried looking around but haven't really got an answer yet. I guess I need
    to write a mini python script. Please don't write any code, I'm just
    looking for a website or something where I can figure this stuff out in
    python.

    Thanks in advance,

    David
    -------
    Cell: http://cellphone.duneram.com/index.html
    Cam: http://www.duneram.com/cam/index.html
    Tax: http://www.duneram.com/index.html

    _______________ _______________ _______________ _______________ _____
    FREE pop-up blocking with the new MSN Toolbar – get it now!



  • Michael Hudson

    #2
    Re: bit shifting question

    "David Stockwell" <winexpert@hotm ail.com> writes:
    [color=blue]
    > Hi,
    > My background is c/c++ and java. I'm learning python at this point.
    >
    > My question is does python share java's peculiar mode of bit shifting,
    > or does python adhere closer to c's bit shifting?[/color]

    The right shift in Python is arithmetic, i.e. preserves the sign bit.
    [color=blue]
    > in java there are 3 kinds of bit shifts:
    > << (shift left)[color=green][color=darkred]
    > >> (preserve the sign bit as we move right )
    > >>> (0 filled on the left as we move right)[/color][/color]
    >
    > In C, the behavior is
    > << (shift left)[color=green][color=darkred]
    > >> (shift right , 0 filled (like java's >>>'))[/color][/color][/color]

    Have you tried this recently? I believe that whether >> sign fills is
    undefined by the C standard but most of the time it dos.
    [color=blue]
    > I tried looking around but haven't really got an answer yet. I guess
    > I need to write a mini python script.[/color]

    No you don't! If you haven't realized that you can do

    $ python
    Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> (-1) >> 3[/color][/color][/color]
    -1

    you have MUCH to learn about Python...

    Cheers,
    mwh

    --
    Python enjoys making tradeoffs that drive *someone* crazy <wink>.
    -- Tim Peters, comp.lang.pytho n

    Comment

    • Grant Edwards

      #3
      Re: bit shifting question

      On 2004-05-17, Michael Hudson <mwh@python.net > wrote:
      [color=blue][color=green]
      >> My question is does python share java's peculiar mode of bit
      >> shifting, or does python adhere closer to c's bit shifting?[/color]
      >
      > The right shift in Python is arithmetic, i.e. preserves the
      > sign bit.[/color]

      Which one is the sign bit? IOW, how wide is a "word"?

      --
      Grant Edwards grante Yow! LBJ, LBJ, how many
      at JOKES did you tell today??!
      visi.com

      Comment

      • Erik Max Francis

        #4
        Re: bit shifting question

        Michael Hudson wrote:
        [color=blue]
        > Have you tried this recently? I believe that whether >> sign fills is
        > undefined by the C standard but most of the time it dos.[/color]

        It's implementation defined, to be more exact.

        --
        __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
        \__/ But you're not going to be there tomorrow. And it's all about
        tomorrow. -- Montgomery Brogan

        Comment

        • Scott David Daniels

          #5
          Re: bit shifting question

          Grant Edwards wrote:
          [color=blue]
          > On 2004-05-17, Michael Hudson <mwh@python.net > wrote:
          >[color=green][color=darkred]
          >>>My question is does python share java's peculiar mode of bit
          >>>shifting, or does python adhere closer to c's bit shifting?[/color]
          >>
          >>The right shift in Python is arithmetic, i.e. preserves the
          >>sign bit.[/color]
          >
          > Which one is the sign bit? IOW, how wide is a "word"?[/color]
          In most cases, you should not be able to discover how wide
          a "word" is. Python is trying to move to a single integer
          data type which is a combination of the current "long" and
          "int" where the internal representation is an implementation
          detail. Unfortunately, shifting is one of those places you
          can still discover the "width" with code like:

          def wordwidth():
          for shift in range(256):
          if 1 << shift < 0:
          return 1 + shift

          --
          -Scott David Daniels
          Scott.Daniels@A cm.Org

          Comment

          • David Eppstein

            #6
            Re: bit shifting question

            In article <40a92d88$1@nnt p0.pdx.net>,
            Scott David Daniels <Scott.Daniels@ Acm.Org> wrote:
            [color=blue]
            > Unfortunately, shifting is one of those places you
            > can still discover the "width" with code like:
            >
            > def wordwidth():
            > for shift in range(256):
            > if 1 << shift < 0:
            > return 1 + shift[/color]

            Of course this will give a warning in 2.3 and fail to work in 2.4...
            I wish I could get the 2.4 behavior now with a from __future__ import
            but I don't see one for this, instead I find myself doing 1L<<shift a
            lot.

            --
            David Eppstein http://www.ics.uci.edu/~eppstein/
            Univ. of California, Irvine, School of Information & Computer Science

            Comment

            • Michael Hudson

              #7
              Re: bit shifting question

              Grant Edwards <grante@visi.co m> writes:
              [color=blue]
              > On 2004-05-17, Michael Hudson <mwh@python.net > wrote:
              >[color=green][color=darkred]
              > >> My question is does python share java's peculiar mode of bit
              > >> shifting, or does python adhere closer to c's bit shifting?[/color]
              > >
              > > The right shift in Python is arithmetic, i.e. preserves the
              > > sign bit.[/color]
              >
              > Which one is the sign bit? IOW, how wide is a "word"?[/color]

              Well, quite :-) Perhaps I should have said "preserves sign".

              Cheers,
              mwh

              --
              I'm sorry, was my bias showing again? :-)
              -- William Tanksley, 13 May 2000

              Comment

              Working...