.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Count the number of bit

Posted By:SanthoshNathan       Posted Date: November 01, 2010    Points: 15    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Count the number of bit
 

using System;
using System.Collections.Generic;
using System.Text;

namespace ExtensionLibrary.Tools
{
    public static class BitOperator
    {
        #region Count the number of bit one 

        public static int GetCountOfBitOne(sbyte x)
        {
            int result = 0;
            while (x != 0)
            {
                result++;
                x &= (sbyte)(x - 1);
            }
            return result;
        }

        public static int GetCountOfBitOne(short x)
        {
            int result = 0;
            while (x != 0)
            {
                result++;
                x &= (short)(x - 1);
            }
            return result;
        }

        public static int GetCountOfBitOne(int x)
        {
            int result = 0;
            while (x != 0)
            {
                result++;
                x &= (x - 1);
            }
            return result;
        }

        public static int GetCountOfBitOne(long x)
        {
            int result = 0;
            while (x!=0)
            {
                result++;
                x &= (x - 1);
            }
            return result;
        }

        public static int GetCountOfBitOne(byte x)
        {
            int result = 0;
            while (x != 0)
            {
                result++;
                x &= (byte)(x - 1);
            }
            return result;
        }

        public static int GetCountOfBitOne(ushort x)
        {
            int result = 0;
            while (x != 0)
            {
                result++;
                x &= (ushort)(x - 1);
            }
            return result;
        }

        public static int GetCountOfBitOne(uint x)
        {
            int result = 0;
            while (x != 0)
            {
                result++;
                x &= (x - 1);
            }
            return result;
        }

        public static int GetCountOfBitOne(ulong x)
        {
            int result = 0;
            while (x != 0)
            {
                result++;
                x &= (x - 1);
            }
            return result;
        }

        #endregion

        #region Count the number of bit zero

        public static int GetCountOfBitZero(sbyte x)
        {
            return GetCountOfBitOne(~x);
        }

        public static int GetCountOfBitZero(short x)
        {
            return GetCountOfBitOne(~x);
        }

        public static int GetCountOfBitZero(int x)
        {
            return GetCountOfBitOne(~x);
        }

        public static int GetCountOfBitZero(long x)
        {
            return GetCountOfBitOne(~x);
        }

        public static int GetCountOfBitZero(byte x)
        {
            return GetCountOfBitOne(~x);
        }

        public static int GetCountOfBitZero(ushort x)
        {
            return GetCountOfBitOne(~x);
        }

        public static int GetCountOfBitZero(uint x)
        {
            return GetCountOfBitOne(~x);
        }

        public static int GetCountOfBitZero(ulong x)
        {
            return GetCountOfBitOne(~x);
        }

        #endregion

        #region Get number of leading zero

        public static int GetNumberOfLeadingZero(sbyte x)
        {
            int number = 8;
            
            sbyte y = (sbyte)(x >> 4)
            if (y != 0)
            {
                number -= 4;
                x = y;
            }

            y = (sbyte)(x >> 2);
            if (y != 0)
            {
                number -= 2;
                x = y;
            }

            y = (sbyte)(x >> 1);
            if (y != 0)
            {
                return number - 2;
            }

            return number - x;
        }

        public static int GetNumberOfLeadingZero(short x)
        {
            int number = 16;

            short y = (short)(x >> 8);
            if (y != 0)
            {
                number -= 8;
                x = y;
            }

            y = (short)(x >> 4);
            if (y != 0)
            {
                number -= 4;
                x = y;
            }

            y = (short)(x >> 2);
            if (y != 0)
            {
                number -= 2;
                x = y;
            }

            y = (short)(x >> 1);
            if (y != 0)
            {
                return number - 2;
            }

            return number - x;
        }

        public static int GetNumberOfLeadingZero(int x)
        {
            int number = 32;

            int y = (x >> 16);
            if (y != 0)
            {
                number -= 16;
                x = y;
            }

            y = (x >> 8);
            if (y != 0)
            {
                number -= 8;
                x = y;
            }

            y = (x >> 4);
            if (y != 0)
            {
                number -= 4;
                x = y;
            }

            y = (x >> 2);
            if (y != 0)
            {
                number -= 2;
                x = y;
            }

            y = (x >> 1);
            if (y != 0)
            {
                return number - 2;
            }

            return number - x;
        }

        public static int GetNumberOfLeadingZero(long x)
        {
            int number = 64;

            long y = (x >> 32);
            if (y != 0)
            {
                number -= 32;
                x = y;
            }

            y = (x >> 16);
            if (y != 0)
            {
                number -= 16;
                x = y;
            }

            y = (x >> 8);
            if (y != 0)
            {
                number -= 8;
                x = y;
            }

            y = (x >> 4);
            if (y != 0)
            {
                number -= 4;
                x = y;
            }

            y = (x >> 2);
            if (y != 0)
            {
                number -= 2;
                x = y;
            }

            y = (x >> 1);
            if (y != 0)
            {
                return number - 2;
            }

            return (int)(number - x);
        }

        public static int GetNumberOfLeadingZero(byte x)
        {
            int number = 8;

            byte y = (byte)(x >> 4);
            if (y != 0)
            {
                number -= 4;
                x = y;
            }

            y = (byte)(x >> 2);
            if (y != 0)
            {
                number -= 2;
                x = y;
            }

            y = (byte)(x >> 1);
            if (y != 0)
            {
                return number - 2;
            }

            return number - x;
        }

        public static int GetNumberOfLeadingZero(ushort x)
        {
            int number = 16;

            ushort y = (ushort)(x >> 8);
            if (y != 0)
            {
                number -= 8;
                x = y;
            }

            y = (ushort)(x >> 4);
            if (y != 0)
            {
                number -= 4;
                x = y;
            }

            y = (ushort)(x >> 2);
            if (y != 0)
            {
                number -= 2;
                x = y;
            }

            y = (ushort)(x >> 1);
            if (y != 


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend