Return word where only the lowest set bit in x is set - CSharp System

CSharp examples for System:String Endian

Description

Return word where only the lowest set bit in x is set

Demo Code


using System;/*from   w  w  w  .  j a v  a  2  s.  c om*/

public class Main{
    // Return word where only the lowest set bit in x is set.
      // Return 0 if no bit is set.
      public static UInt32 LowestBit( UInt32 x )
      {
         //    if ( 0==x )  return 0;
         //    return  ((x^(x-1)) >> 1) + 1;
         //    return  (x & (x-1)) ^ x;

         return  x & (UInt32)(-x);  // use: -x == ~x + 1
      }
}

Related Tutorials