Return word where a all bits from (including) the highest set bit to bit 0 are set. - CSharp System

CSharp examples for System:String Endian

Description

Return word where a all bits from (including) the highest set bit to bit 0 are set.

Demo Code


using System;/*from   w w  w  .ja va 2  s  . co m*/

public class Main{
    // Return word where a all bits from (including) the
      //   highest set bit to bit 0 are set.
      // Return 0 if no bit is set.
      //
      // Feed the result into bit_count() to get
      //   the index of the highest bit set.
      public static UInt32 HighestBit01Edge( UInt32 x )
      {
         x |= x>>1;
         x |= x>>2;
         x |= x>>4;
         x |= x>>8;
         x |= x>>16;

         return  x;
      }
}

Related Tutorials