Login | Register

C\C++: Get highest bit '1' in unsigned int.

Visual C++/C/C# related discussions

Moderators: The Coolest, imposter

  • Author
    Message

C\C++: Get highest bit '1' in unsigned int.

Postby The Coolest » Sun Oct 12, 2008 12:20 am

Code: Select all
int GetHighestBit(DWORD_PTR _dwValue)
{
   if (!_dwValue)
      return -1;

   DWORD_PTR dwpCnt;   
   int iRet;

#ifdef _WIN64
   dwpCnt = 0x8000000000000000;
   iRet = 63;
#else
   dwpCnt = 0x80000000;
   iRet = 31;
#endif

   while (!(dwpCnt & _dwValue))
   {
      iRet--;
      dwpCnt = dwpCnt >> 1;
   }

   return iRet;
}


This is what I have right now, any idea how to improve this code? Speed is of no importance since it only executes once in the program.
The solution must not include any extra libraries, since I'm trying to keep the executable small.
Member of our O/C Folding @ Home team
"<The_Coolest> you can't unwaste wasted CPU cycles" - Start FOLDing now!
Main rig:
Core i7 2600K 3.4GHz @ 4.5GHz (Scythe Mugen2) / Mobo: Biostar TP67XE / SSD: Crucial M4 128GB (+ HDDs) / GPU: HD5450 / Mem: 4x4GB DDR3-1600 G.Skill 4GBXL RipJaws X - 16GB total / PSU: Seasonic S12II 620W.
Secondary rig:
Core i3 540 3.06GHz @ Stock / Mobo: MSI H55M-ED55 / GPU: Integrated / Mem: 2x2GB DDR3-1600 G.Skill 4GBRL RipJaws - 4GB total / PSU: FSP ATX350-PNR 350W.

Core Temp - Accurate temperature monitor for Intel's Core/Core 2 and AMD64 processors
User avatar
The Coolest
Site Admin
Site Admin
 
Posts: 2961
Joined: Tue Feb 18, 2003 7:48 pm
Location: Tel Aviv, Israel

Postby hathor » Sun Oct 12, 2008 6:55 pm

Code: Select all
/** Returns the most significant bit set in a value.
        */
        static FORCEINLINE unsigned int mostSignificantBitSet(unsigned int value)
        {
            unsigned int result = 0;
            while (value != 0) {
                ++result;
                value >>= 1;
            }
            return result-1;
        }
Greetings from BERLIN, GERMANY!
tiu-hathor
hathor
Registered User
Registered User
 
Posts: 31
Joined: Fri Aug 25, 2006 7:02 am
Location: Berlin, Germany

Postby The Coolest » Sun Oct 12, 2008 10:30 pm

O damn, looks like I've been looking at it from the wrong angle. :oops:

Thanks :D

Code: Select all
int GetHighestBit(DWORD_PTR _dwValue)
{
   int iRet = 0;

   while (_dwValue)
   {
      iRet++;
      _dwValue >>= 1;
   }

   return --iRet;
}
Member of our O/C Folding @ Home team
"<The_Coolest> you can't unwaste wasted CPU cycles" - Start FOLDing now!
Main rig:
Core i7 2600K 3.4GHz @ 4.5GHz (Scythe Mugen2) / Mobo: Biostar TP67XE / SSD: Crucial M4 128GB (+ HDDs) / GPU: HD5450 / Mem: 4x4GB DDR3-1600 G.Skill 4GBXL RipJaws X - 16GB total / PSU: Seasonic S12II 620W.
Secondary rig:
Core i3 540 3.06GHz @ Stock / Mobo: MSI H55M-ED55 / GPU: Integrated / Mem: 2x2GB DDR3-1600 G.Skill 4GBRL RipJaws - 4GB total / PSU: FSP ATX350-PNR 350W.

Core Temp - Accurate temperature monitor for Intel's Core/Core 2 and AMD64 processors
User avatar
The Coolest
Site Admin
Site Admin
 
Posts: 2961
Joined: Tue Feb 18, 2003 7:48 pm
Location: Tel Aviv, Israel

Return to Visual C++/C/C#

Who is online

Users browsing this forum: No registered users and 1 guest

cron