x&(x-1) ... why only 2's complement system?

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

    x&(x-1) ... why only 2's complement system?


    Why is the "x&(x-1)" trick for removing the least significant set bit
    from an integer only valid on 2's complement systems?

  • Eric Sosman

    #2
    Re: x&(x-1) ... why only 2's complement system?

    Lax wrote:
    Why is the "x&(x-1)" trick for removing the least significant set bit
    from an integer only valid on 2's complement systems?
    It is valid on all systems if `x' is non-negative.
    That means, as a particularly useful special case, that
    it is always valid if `x' is unsigned.

    For negative values of `x', I suggest you take pencil
    and paper and work through a few examples, using all three
    of the representations C allows: two's complement, ones'
    complement, and signed magnitude. To save some writing,
    pretend your computer uses a narrow int of maybe six or
    eight bits. Try a few different `x' values like -1, -2,
    -10, and see what happens.

    --
    Eric.Sosman@sun .com

    Comment

    • Lax

      #3
      Re: x&(x-1) ... why only 2's complement system?

      On Apr 9, 5:09 pm, Lax <Lax.Cla...@gma il.comwrote:
      Why is the "x&(x-1)" trick for removing the least significant set bit
      from an integer only valid on 2's complement systems?
      Thank you all (for suggesting and showing counterexamples ).

      So is this a good implementation-independent way of doing it?

      (signed)( x & ( (unsigned)x-1 ) )

      Comment

      • Peter Nilsson

        #4
        Re: x&amp;(x-1) ... why only 2's complement system?

        Lax wrote:
        Lax <Lax.Cla...@gma il.comwrote:
        Why is the "x&(x-1)" trick for removing the least
        significant set bit from an integer only valid on 2's
        complement systems?
        >
        Thank you all (for suggesting and showing
        counterexamples ).
        >
        So is this a good implementation-independent way of doing it?
        >
        (signed)( x & ( (unsigned)x-1 ) )
        No, the right way is to make x unsigned to begin with, and
        to forget about testing bits in negative integers.

        --
        Peter

        Comment

        • CBFalconer

          #5
          Re: x&amp;(x-1) ... why only 2's complement system?

          Lax wrote:
          >
          Why is the "x&(x-1)" trick for removing the least significant set
          bit from an integer only valid on 2's complement systems?
          It works fine on unsigned integers also. Just write down the bit
          patterns involved.

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.


          ** Posted from http://www.teranews.com **

          Comment

          • CBFalconer

            #6
            Re: x&amp;(x-1) ... why only 2's complement system?

            Lax wrote:
            Lax <Lax.Cla...@gma il.comwrote:
            >
            >Why is the "x&(x-1)" trick for removing the least significant set
            >bit from an integer only valid on 2's complement systems?
            >
            Thank you all (for suggesting and showing counterexamples ).
            So is this a good implementation-independent way of doing it?
            >
            (signed)( x & ( (unsigned)x-1 ) )
            NO. It is a good way of getting undefined behaviour.

            --
            [mail]: Chuck F (cbfalconer at maineline dot net)
            [page]: <http://cbfalconer.home .att.net>
            Try the download section.

            ** Posted from http://www.teranews.com **

            Comment

            • Falcon Kirtaran

              #7
              Re: x&amp;(x-1) ... why only 2's complement system?

              Eric Sosman wrote:
              Lax wrote:
              >Why is the "x&(x-1)" trick for removing the least significant set bit
              >from an integer only valid on 2's complement systems?
              >
              It is valid on all systems if `x' is non-negative.
              That means, as a particularly useful special case, that
              it is always valid if `x' is unsigned.
              >
              For negative values of `x', I suggest you take pencil
              and paper and work through a few examples, using all three
              of the representations C allows: two's complement, ones'
              complement, and signed magnitude. To save some writing,
              pretend your computer uses a narrow int of maybe six or
              eight bits. Try a few different `x' values like -1, -2,
              -10, and see what happens.
              >
              Hmm. I stand corrected.

              --
              --Falcon Kirtaran

              Comment

              Working...