Binary question

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

    #1

    Binary question

    Given any arbitrary byte (or int for that matter), is there a way to figure
    out:
    1) The *number of one's set without a loop? Maybe some formula?.
    2) Looking from left the right, if all 1's are continuous with not
    intervening 0's. 0's on the end ok.

    TIA
    --
    William Stacey, MVP



  • Jens Thiel

    #2
    Re: Binary question

    "William Stacey" <staceywREMOVE@ mvps.org> wrote:[color=blue]
    > Given any arbitrary byte (or int for that matter), is there a way to[/color]
    figure[color=blue]
    > out:
    > 1) The *number of one's set without a loop? Maybe some formula?.
    > 2) Looking from left the right, if all 1's are continuous with not
    > intervening 0's. 0's on the end ok.[/color]

    I would use an array that is filled with the required information at compile
    time, and the byte as index to the array at run-time.

    Jens.

    --
    http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
    Replace MSDN with my first name when replying to my email address!


    Comment

    • William Stacey

      #3
      Re: Binary question

      Yes I could do that and thanks for the reply. I was just thinking this
      could be done with math.

      --
      --
      William Stacey, MVP

      "Jens Thiel" <MSDN@Thiel.d e> wrote in message
      news:#DKQfzotDH A.980@TK2MSFTNG P10.phx.gbl...[color=blue]
      > "William Stacey" <staceywREMOVE@ mvps.org> wrote:[color=green]
      > > Given any arbitrary byte (or int for that matter), is there a way to[/color]
      > figure[color=green]
      > > out:
      > > 1) The *number of one's set without a loop? Maybe some formula?.
      > > 2) Looking from left the right, if all 1's are continuous with not
      > > intervening 0's. 0's on the end ok.[/color]
      >
      > I would use an array that is filled with the required information at[/color]
      compile[color=blue]
      > time, and the byte as index to the array at run-time.
      >
      > Jens.
      >
      > --
      > http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
      > Replace MSDN with my first name when replying to my email address!
      >
      >[/color]


      Comment

      • William Stacey

        #4
        Re: Binary question

        FYI. Did some more testings and research and came up with this. Any
        optimizations?

        /// <summary>
        /// Returns the number of "1" bits set in any uint.
        /// </summary>
        public static int BitCount(uint x)
        {
        int bitCount = 0;

        if ( x != 0)
        {
        do
        {
        bitCount++;
        x = x & (x - 1);
        } while (x != 0);
        }
        return bitCount;
        }

        /// <summary>
        /// Given any uint, returns true if the 1 bits are contiguous from left to
        right.
        /// This method will also return to the "out" parameter the number of 1 bits
        in
        /// the num, even if the bits are not contiguous.
        /// </summary>
        public static bool ContiguousBits( uint num, out int bitCount)
        {
        int shift = 0;
        int bc = 0;

        bc = Bits.BitCount(n um); //The BitCount Method above.
        shift = 32 - bc;
        bitCount = bc;
        return ( (num >> shift) == (Math.Pow(2, bc) - 1) );
        }

        --
        William Stacey, MVP

        "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
        news:uLxtokotDH A.536@tk2msftng p13.phx.gbl...[color=blue]
        > Given any arbitrary byte (or int for that matter), is there a way to[/color]
        figure[color=blue]
        > out:
        > 1) The *number of one's set without a loop? Maybe some formula?.
        > 2) Looking from left the right, if all 1's are continuous with not
        > intervening 0's. 0's on the end ok.
        >
        > TIA
        > --
        > William Stacey, MVP
        >
        >
        >[/color]


        Comment

        • Vijaye Raji

          #5
          Re: Binary question

          Your question was - 'without using a loop'....

          The fastest algorithm, if using Assembly, would be to use one register and
          loop a right shift operation and add the flag bit everytime.

          However, the fastest, if holding an array of lookup is not a problem, would
          be to construct a 256 lookup table upfront and take 8bits at a time and look
          up 4 times for a 32 bit number.

          vJ

          "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
          news:uq8DAjqtDH A.1088@tk2msftn gp13.phx.gbl...[color=blue]
          > FYI. Did some more testings and research and came up with this. Any
          > optimizations?
          >
          > /// <summary>
          > /// Returns the number of "1" bits set in any uint.
          > /// </summary>
          > public static int BitCount(uint x)
          > {
          > int bitCount = 0;
          >
          > if ( x != 0)
          > {
          > do
          > {
          > bitCount++;
          > x = x & (x - 1);
          > } while (x != 0);
          > }
          > return bitCount;
          > }
          >
          > /// <summary>
          > /// Given any uint, returns true if the 1 bits are contiguous from left to
          > right.
          > /// This method will also return to the "out" parameter the number of 1[/color]
          bits[color=blue]
          > in
          > /// the num, even if the bits are not contiguous.
          > /// </summary>
          > public static bool ContiguousBits( uint num, out int bitCount)
          > {
          > int shift = 0;
          > int bc = 0;
          >
          > bc = Bits.BitCount(n um); //The BitCount Method above.
          > shift = 32 - bc;
          > bitCount = bc;
          > return ( (num >> shift) == (Math.Pow(2, bc) - 1) );
          > }
          >
          > --
          > William Stacey, MVP
          >
          > "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
          > news:uLxtokotDH A.536@tk2msftng p13.phx.gbl...[color=green]
          > > Given any arbitrary byte (or int for that matter), is there a way to[/color]
          > figure[color=green]
          > > out:
          > > 1) The *number of one's set without a loop? Maybe some formula?.
          > > 2) Looking from left the right, if all 1's are continuous with not
          > > intervening 0's. 0's on the end ok.
          > >
          > > TIA
          > > --
          > > William Stacey, MVP
          > >
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • ozbear

            #6
            Re: Binary question

            On Sat, 29 Nov 2003 09:57:35 -0500, "William Stacey"
            <staceywREMOVE@ mvps.org> wrote:
            [color=blue]
            >Given any arbitrary byte (or int for that matter), is there a way to figure
            >out:
            >1) The *number of one's set without a loop? Maybe some formula?.
            >2) Looking from left the right, if all 1's are continuous with not
            >intervening 0's. 0's on the end ok.
            >
            >TIA
            >--
            >William Stacey, MVP
            >
            >
            >[/color]

            For (1) above you could use...

            int bitcount(int i)
            {
            i = (i & 0x55555555) + ((i>>1) & 0x55555555);
            i = (i & 0x33333333) + ((i>>2) & 0x33333333);
            i = (i & 0x0F0F0F0F) + ((i>>4) & 0x0F0F0F0F);
            return (i % 255);
            }

            Oz

            Comment

            • Jens Thiel

              #7
              Re: Binary question

              "William Stacey" <staceywREMOVE@ mvps.org> wrote:[color=blue]
              > do
              > {
              > bitCount++;
              > x = x & (x - 1);
              > } while (x != 0);[/color]

              looks very nice! Someone told me once that the NSA requested Cray to
              implement BitCount() in hardware in the early days, and that all their
              machines had this unit.
              [color=blue]
              > public static bool ContiguousBits( uint num, out int bitCount)
              > {
              > int shift = 0;
              > int bc = 0;
              >
              > bc = Bits.BitCount(n um); //The BitCount Method above.
              > shift = 32 - bc;
              > bitCount = bc;
              > return ( (num >> shift) == (Math.Pow(2, bc) - 1) );
              > }
              >[/color]

              Wouldn't it be sufficient to test

              0 == (0xFFFFFFFF>>bc ) & num

              once you have the bit count?

              x86 assembly has BSF/BSR which you could exploit...

              Jens.

              --
              http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
              Replace MSDN with my first name when replying to my email address!


              Comment

              • William Stacey

                #8
                Re: Binary question

                Thanks Ozbear. That looks faster yet. Cheers!

                --
                William Stacey, MVP

                "ozbear" <ozbear2@yahoo. com> wrote in message
                news:3fc911d0.6 33029062@news-server...[color=blue]
                > On Sat, 29 Nov 2003 09:57:35 -0500, "William Stacey"
                > <staceywREMOVE@ mvps.org> wrote:
                >[color=green]
                > >Given any arbitrary byte (or int for that matter), is there a way to[/color][/color]
                figure[color=blue][color=green]
                > >out:
                > >1) The *number of one's set without a loop? Maybe some formula?.
                > >2) Looking from left the right, if all 1's are continuous with not
                > >intervening 0's. 0's on the end ok.
                > >
                > >TIA
                > >--
                > >William Stacey, MVP
                > >
                > >
                > >[/color]
                >
                > For (1) above you could use...
                >
                > int bitcount(int i)
                > {
                > i = (i & 0x55555555) + ((i>>1) & 0x55555555);
                > i = (i & 0x33333333) + ((i>>2) & 0x33333333);
                > i = (i & 0x0F0F0F0F) + ((i>>4) & 0x0F0F0F0F);
                > return (i % 255);
                > }
                >
                > Oz[/color]


                Comment

                • William Stacey

                  #9
                  Re: Binary question

                  > Wouldn't it be sufficient to test[color=blue]
                  >
                  > 0 == (0xFFFFFFFF>>bc ) & num
                  >
                  > once you have the bit count?[/color]

                  Great idea. Thanks Jens. Cheers!
                  --
                  wjs


                  Comment

                  • William Stacey

                    #10
                    Re: Binary question

                    BTW - any chance you have a few lines that will set a range of bits to a
                    supplied bit mask. Maybe with a sig like:
                    int SetBits(uint src, int pos, int count, uint bitMask)
                    {
                    ...
                    return x; //Returns the new uint with the bitMask (1's and/or 0's)
                    applied to the pos with max count bits.
                    }
                    TIA again :-)

                    --
                    --
                    William Stacey, MVP

                    "Jens Thiel" <MSDN@Thiel.d e> wrote in message
                    news:#Y9zzUstDH A.2368@TK2MSFTN GP09.phx.gbl...[color=blue]
                    > "William Stacey" <staceywREMOVE@ mvps.org> wrote:[color=green]
                    > > do
                    > > {
                    > > bitCount++;
                    > > x = x & (x - 1);
                    > > } while (x != 0);[/color]
                    >
                    > looks very nice! Someone told me once that the NSA requested Cray to
                    > implement BitCount() in hardware in the early days, and that all their
                    > machines had this unit.
                    >[color=green]
                    > > public static bool ContiguousBits( uint num, out int bitCount)
                    > > {
                    > > int shift = 0;
                    > > int bc = 0;
                    > >
                    > > bc = Bits.BitCount(n um); //The BitCount Method above.
                    > > shift = 32 - bc;
                    > > bitCount = bc;
                    > > return ( (num >> shift) == (Math.Pow(2, bc) - 1) );
                    > > }
                    > >[/color]
                    >
                    > Wouldn't it be sufficient to test
                    >
                    > 0 == (0xFFFFFFFF>>bc ) & num
                    >
                    > once you have the bit count?
                    >
                    > x86 assembly has BSF/BSR which you could exploit...
                    >
                    > Jens.
                    >
                    > --
                    > http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
                    > Replace MSDN with my first name when replying to my email address!
                    >
                    >[/color]


                    Comment

                    • Jeffrey Tan[MSFT]

                      #11
                      Re: Binary question


                      Hi William,

                      Based on my understanding, you want to get part of bitMask's(From right to
                      left: [pos, pos+count]) and apply it & with num.
                      So you can do like this:
                      int SetBits(uint src, int pos, int count, uint bitMask)
                      {
                      uint centerMask=((0x FFFFFFFF<<(32-count))>>(32-pos-count));
                      x=((centerMask& bitMask)&src);
                      return x;
                      }

                      If I misunderstand you, please feel free to tell me.

                      Best regards,
                      Jeffrey Tan
                      Microsoft Online Partner Support
                      Get Secure! - www.microsoft.com/security
                      This posting is provided "as is" with no warranties and confers no rights.

                      Comment

                      • William Stacey

                        #12
                        Re: Binary question

                        Thanks Jeffrey.



                        Comment

                        Working...