odd/even bitwise and

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

    #1

    odd/even bitwise and

    Some people prefer to use
    "if (x & 1)"
    to see if a number is odd or even. Is this completely portable according to
    the standard?


  • Ahmed S. Badran

    #2
    Re: odd/even bitwise and


    "Serve Laurijssen" <cs@nospam.comp .com> wrote in message
    news:eJucc.5394 $RU5.83970@zonn et-reader-1...[color=blue]
    > "if (x & 1)"
    > to see if a number is odd or even. Is this completely portable according[/color]
    to[color=blue]
    > the standard?[/color]

    I don't know what does this have to do with the standard, but the thing is
    an odd number will ALWAYS have bit 0 set to '1' and an even number will
    always have bit 0 set to '0'. This is a matter of binary representation.

    Ahmed


    Comment

    • Ahmed S. Badran

      #3
      Re: odd/even bitwise and


      "Serve Laurijssen" <cs@nospam.comp .com> wrote in message
      news:eJucc.5394 $RU5.83970@zonn et-reader-1...[color=blue]
      > "if (x & 1)"
      > to see if a number is odd or even. Is this completely portable according[/color]
      to[color=blue]
      > the standard?[/color]

      I don't know what does this have to do with the standard, but the thing is
      an odd number will ALWAYS have bit 0 set to '1' and an even number will
      always have bit 0 set to '0'. This is a matter of binary representation.

      Ahmed


      Comment

      • CBFalconer

        #4
        Re: odd/even bitwise and

        Serve Laurijssen wrote:[color=blue]
        >
        > Some people prefer to use
        > "if (x & 1)"
        > to see if a number is odd or even. Is this completely portable
        > according to the standard?[/color]

        Only if x is some form of unsigned integer.

        --
        Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net> USE worldnet address!


        Comment

        • CBFalconer

          #5
          Re: odd/even bitwise and

          Serve Laurijssen wrote:[color=blue]
          >
          > Some people prefer to use
          > "if (x & 1)"
          > to see if a number is odd or even. Is this completely portable
          > according to the standard?[/color]

          Only if x is some form of unsigned integer.

          --
          Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
          Available for consulting/temporary embedded and systems.
          <http://cbfalconer.home .att.net> USE worldnet address!


          Comment

          • pete

            #6
            Re: odd/even bitwise and

            Serve Laurijssen wrote:[color=blue]
            >
            > Some people prefer to use
            > "if (x & 1)"
            > to see if a number is odd or even.
            > Is this completely portable according to the standard?[/color]

            If x is an unsigned integer type
            or a signed type with a non negative value, then it's portable.

            --
            pete

            Comment

            • pete

              #7
              Re: odd/even bitwise and

              Serve Laurijssen wrote:[color=blue]
              >
              > Some people prefer to use
              > "if (x & 1)"
              > to see if a number is odd or even.
              > Is this completely portable according to the standard?[/color]

              If x is an unsigned integer type
              or a signed type with a non negative value, then it's portable.

              --
              pete

              Comment

              • pete

                #8
                Re: odd/even bitwise and

                Ahmed S. Badran wrote:[color=blue]
                >
                > "Serve Laurijssen" <cs@nospam.comp .com> wrote in message
                > news:eJucc.5394 $RU5.83970@zonn et-reader-1...[color=green]
                > > "if (x & 1)"
                > > to see if a number is odd or even.
                > > Is this completely portable according to the standard?[/color]
                >
                > I don't know what does this have to do with the standard,[/color]

                The answer to the question, is in the standard.
                [color=blue]
                > but the thing is an odd number will ALWAYS have bit 0 set to
                > '1' and an even number will always have bit 0 set to '0'.[/color]

                That's not true for one's complement
                representations of negative integers.
                [color=blue]
                > This is a matter of binary representation.[/color]

                The standard specifies more than one
                way to represent negative integers.

                --
                pete

                Comment

                • pete

                  #9
                  Re: odd/even bitwise and

                  Ahmed S. Badran wrote:[color=blue]
                  >
                  > "Serve Laurijssen" <cs@nospam.comp .com> wrote in message
                  > news:eJucc.5394 $RU5.83970@zonn et-reader-1...[color=green]
                  > > "if (x & 1)"
                  > > to see if a number is odd or even.
                  > > Is this completely portable according to the standard?[/color]
                  >
                  > I don't know what does this have to do with the standard,[/color]

                  The answer to the question, is in the standard.
                  [color=blue]
                  > but the thing is an odd number will ALWAYS have bit 0 set to
                  > '1' and an even number will always have bit 0 set to '0'.[/color]

                  That's not true for one's complement
                  representations of negative integers.
                  [color=blue]
                  > This is a matter of binary representation.[/color]

                  The standard specifies more than one
                  way to represent negative integers.

                  --
                  pete

                  Comment

                  • osmium

                    #10
                    Re: odd/even bitwise and

                    Ahmed S. Badran writes:
                    [color=blue][color=green]
                    > > "if (x & 1)"
                    > > to see if a number is odd or even. Is this completely portable according[/color]
                    > to[color=green]
                    > > the standard?[/color]
                    >
                    > I don't know what does this have to do with the standard, but the thing is
                    > an odd number will ALWAYS have bit 0 set to '1' and an even number will
                    > always have bit 0 set to '0'. This is a matter of binary representation.[/color]

                    It depends on the hardware. If x is a one's complement representation of 0
                    the test might fail. It strikes me as code intended to impress someone with
                    one's erudition, I would not do it.


                    Comment

                    • osmium

                      #11
                      Re: odd/even bitwise and

                      Ahmed S. Badran writes:
                      [color=blue][color=green]
                      > > "if (x & 1)"
                      > > to see if a number is odd or even. Is this completely portable according[/color]
                      > to[color=green]
                      > > the standard?[/color]
                      >
                      > I don't know what does this have to do with the standard, but the thing is
                      > an odd number will ALWAYS have bit 0 set to '1' and an even number will
                      > always have bit 0 set to '0'. This is a matter of binary representation.[/color]

                      It depends on the hardware. If x is a one's complement representation of 0
                      the test might fail. It strikes me as code intended to impress someone with
                      one's erudition, I would not do it.


                      Comment

                      • Ahmed S. Badran

                        #12
                        Re: odd/even bitwise and

                        pete wrote:[color=blue]
                        > Ahmed S. Badran wrote:[color=green]
                        >>
                        >> "Serve Laurijssen" <cs@nospam.comp .com> wrote in message
                        >> news:eJucc.5394 $RU5.83970@zonn et-reader-1...[color=darkred]
                        >>> "if (x & 1)"
                        >>> to see if a number is odd or even.
                        >>> Is this completely portable according to the standard?[/color]
                        >>
                        >> I don't know what does this have to do with the standard,[/color]
                        >
                        > The answer to the question, is in the standard.
                        >[color=green]
                        >> but the thing is an odd number will ALWAYS have bit 0 set to
                        >> '1' and an even number will always have bit 0 set to '0'.[/color]
                        >
                        > That's not true for one's complement
                        > representations of negative integers.
                        >[color=green]
                        >> This is a matter of binary representation.[/color]
                        >
                        > The standard specifies more than one
                        > way to represent negative integers.[/color]
                        Ok, just to re-elaborate and make the answer complete, I never took negative
                        numbers into consideration with my previous answer, so my previous answer is
                        valid/correct with all positive integers. Thanks for pointing that out guys.

                        Ahmed



                        Comment

                        • Ahmed S. Badran

                          #13
                          Re: odd/even bitwise and

                          pete wrote:[color=blue]
                          > Ahmed S. Badran wrote:[color=green]
                          >>
                          >> "Serve Laurijssen" <cs@nospam.comp .com> wrote in message
                          >> news:eJucc.5394 $RU5.83970@zonn et-reader-1...[color=darkred]
                          >>> "if (x & 1)"
                          >>> to see if a number is odd or even.
                          >>> Is this completely portable according to the standard?[/color]
                          >>
                          >> I don't know what does this have to do with the standard,[/color]
                          >
                          > The answer to the question, is in the standard.
                          >[color=green]
                          >> but the thing is an odd number will ALWAYS have bit 0 set to
                          >> '1' and an even number will always have bit 0 set to '0'.[/color]
                          >
                          > That's not true for one's complement
                          > representations of negative integers.
                          >[color=green]
                          >> This is a matter of binary representation.[/color]
                          >
                          > The standard specifies more than one
                          > way to represent negative integers.[/color]
                          Ok, just to re-elaborate and make the answer complete, I never took negative
                          numbers into consideration with my previous answer, so my previous answer is
                          valid/correct with all positive integers. Thanks for pointing that out guys.

                          Ahmed



                          Comment

                          • Rogério Brito

                            #14
                            Re: odd/even bitwise and

                            Serve Laurijssen wrote:[color=blue]
                            > Some people prefer to use
                            > "if (x & 1)"
                            > to see if a number is odd or even. Is this completely portable according to
                            > the standard?[/color]

                            No. That is only possible if the representation of the integers use
                            twos-complement.

                            To see if a number is odd or even, use the modulus operator (e.g., x%2).

                            --
                            =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                            Rogério Brito - rbrito@ime.usp. br - http://www.ime.usp.br/~rbrito
                            =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                            Comment

                            • Rogério Brito

                              #15
                              Re: odd/even bitwise and

                              Serve Laurijssen wrote:[color=blue]
                              > Some people prefer to use
                              > "if (x & 1)"
                              > to see if a number is odd or even. Is this completely portable according to
                              > the standard?[/color]

                              No. That is only possible if the representation of the integers use
                              twos-complement.

                              To see if a number is odd or even, use the modulus operator (e.g., x%2).

                              --
                              =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                              Rogério Brito - rbrito@ime.usp. br - http://www.ime.usp.br/~rbrito
                              =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                              Comment

                              Working...