((i & 1) == 1)

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

    ((i & 1) == 1)

    public static bool isodd(int i)
    {
    return ((i & 1) == 1);
    }

    can someone explain me how this is working
  • Tom Shelton

    #2
    Re: ((i & 1) == 1)

    On 2008-06-26, joso <joso@hi.t-com.hrwrote:
    public static bool isodd(int i)
    {
    return ((i & 1) == 1);
    }
    >
    can someone explain me how this is working
    It's a simple test for oddness on an integer value. Any integer value that is
    odd will have a one in the ones column... So, the result of a bitwise and of
    any integer value and one, can tell you if it is odd or not.

    1111 & 0001 = 1
    0111 & 0001 = 1
    0100 & 0001 = 0
    1110 & 0001 = 0

    --
    Tom Shelton

    Comment

    • Bob Powell [MVP]

      #3
      Re: ((i &amp; 1) == 1)

      An integer is a basic type having a number of binary bits.

      Depending on your processer architecture this might be 32 or 64 bits.

      The binary and (&) operator performs a bitwise "AND" operation on the
      integer which effectively removes all but the least significant bit of the
      integer. This may be 1 or 0 depending on the value.

      In the case that it is indeed 1, the number is odd.

      Hope this was easily understood.

      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Ramuseco Limited .NET consulting


      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.

      "joso" <joso@hi.t-com.hrwrote in message
      news:g410fi$jac $1@ss408.t-com.hr...
      public static bool isodd(int i)
      {
      return ((i & 1) == 1);
      }
      >
      can someone explain me how this is working

      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: ((i &amp; 1) == 1)

        Bob Powell [MVP] wrote:
        An integer is a basic type having a number of binary bits.
        >
        Depending on your processer architecture this might be 32 or 64 bits.
        In C# an int is always 32 bit.

        Arne

        Comment

        • Joe Fawcett

          #5
          Re: ((i &amp; 1) == 1)



          "Arne Vajhøj" <arne@vajhoej.d kwrote in message
          news:486436bf$0 $90270$14726298 @news.sunsite.d k...
          Bob Powell [MVP] wrote:
          >An integer is a basic type having a number of binary bits.
          >>
          >Depending on your processer architecture this might be 32 or 64 bits.
          >
          In C# an int is always 32 bit.
          >
          Arne
          He didn't write 'int' but 'integer'. An integer is a mathematical concept,
          an int in C# is an alias for a System.Int32 which is an integer represented
          in the architecture as 32 bits as opposed to an Int64.

          --

          Joe Fawcett (MVP - XML)


          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: ((i &amp; 1) == 1)

            Joe Fawcett wrote:
            "Arne Vajhøj" <arne@vajhoej.d kwrote in message
            news:486436bf$0 $90270$14726298 @news.sunsite.d k...
            >Bob Powell [MVP] wrote:
            >>An integer is a basic type having a number of binary bits.
            >>>
            >>Depending on your processer architecture this might be 32 or 64 bits.
            >>
            >In C# an int is always 32 bit.
            He didn't write 'int' but 'integer'. An integer is a mathematical
            concept, an int in C# is an alias for a System.Int32 which is an integer
            represented in the architecture as 32 bits as opposed to an Int64.
            How do you think that he think about integer as a mathematical type
            when he write "An integer is a basic type having a number of binary
            bits" ?

            Besides it would be even worse if it were the case. Believing that
            a mathematical concept depends on processor architecture to be
            either 32 or 64 bit.

            Arne


            Comment

            Working...