Quantcast
Channel: What is the fastest/most efficient way to find the position of the highest set bit (msb) in an integer in C? - Stack Overflow
Browsing index pages (73 articles)

What is the fastest/most efficient way to find the position of the highest...

If I have some integer n, and I want to know the position of the most significant bit (that is, if the least significant bit is on the right, I want to know the position of the farthest left bit that...

View Article


Answer by Noldorin for What is the fastest/most efficient way to find the...

Although I would probably only use this method if I absolutely required the best possible performance (e.g. for writing some sort of board game AI involving bitboards), the most efficient solution is...

View Article


Answer by SPWorley for What is the fastest/most efficient way to find the...

This is sort of like finding a kind of integer log. There are bit-twiddling tricks, but I've made my own tool for this. The goal of course is for speed. My realization is that the CPU has an automatic...

View Article

Answer by Vasil for What is the fastest/most efficient way to find the...

Think bitwise operators.I missunderstood the question the first time. You should produce an int with the leftmost bit set (the others zero). Assuming cmp is set to that value:position =...

View Article

Answer by timday for What is the fastest/most efficient way to find the...

Assuming you're on x86 and game for a bit of inline assembler, Intel provides a BSR instruction ("bit scan reverse"). It's fast on some x86s (microcoded on others). From the manual:Searches the source...

View Article


Answer by Protagonist for What is the fastest/most efficient way to find the...

This should be lightning fast:int msb(unsigned int v) { static const int pos[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10,...

View Article

Answer by rlbond for What is the fastest/most efficient way to find the...

unsigned intmsb32(register unsigned int x){ x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); x |= (x >> 8); x |= (x >> 16); return(x & ~(x >> 1));}1 register, 13...

View Article

Answer by ephemient for What is the fastest/most efficient way to find the...

GCC has: -- Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in X, starting at the most significant bit position. If X is 0, the result is undefined. --...

View Article


Answer by Quinn Taylor for What is the fastest/most efficient way to find the...

Since 2^N is an integer with only the Nth bit set (1 << N), finding the position (N) of the highest set bit is the integer log base 2 of that...

View Article


Answer by Josh for What is the fastest/most efficient way to find the...

Here are some (simple) benchmarks, of algorithms currently given on this page...The algorithms have not been tested over all inputs of unsigned int; so check that first, before blindly using something...

View Article

Answer by JonesD for What is the fastest/most efficient way to find the...

Expanding on Josh's benchmark...one can improve the clz as follows/***************** clz2 ********************/#define NUM_OF_HIGHESTBITclz2(a) ((a) \ ? (((1U) << (sizeof(unsigned)*8-1)) >>...

View Article

Answer by dangermouse for What is the fastest/most efficient way to find the...

I had a need for a routine to do this and before searching the web (and finding this page) I came up with my own solution basedon a binary search. Although I'm sure someone has done this before! It...

View Article

Answer by Kaz for What is the fastest/most efficient way to find the position...

Kaz Kylheku here I benchmarked two approaches for this over 63 bit numbers (the long long type on gcc x86_64), staying away from the sign bit.(I happen to need this "find highest bit" for something,...

View Article


Answer by greggo for What is the fastest/most efficient way to find the...

Putting this in since it's 'yet another' approach, seems to be different from others already given.returns -1 if x==0, otherwise floor( log2(x)) (max result 31)Reduce from 32 to 4 bit problem, then use...

View Article

Answer by Marco Amagliani for What is the fastest/most efficient way to find...

What aboutint highest_bit(unsigned int a) { int count; std::frexp(a, &count); return count - 1;}?

View Article


Answer by David C. Rankin for What is the fastest/most efficient way to find...

As the answers above point out, there are a number of ways to determine the most significant bit. However, as was also pointed out, the methods are likely to be unique to either 32bit or 64bit...

View Article

Answer by user3177100 for What is the fastest/most efficient way to find the...

A version in C using successive approximation:unsigned int getMsb(unsigned int n){ unsigned int msb = sizeof(n) * 4; unsigned int step = msb; while (step > 1) { step /=2; if (n>>msb) msb +=...

View Article


Answer by Jonathan Mee for What is the fastest/most efficient way to find the...

c99 has given us log2. This removes the need for all the special sauce log2 implementations you see on this page. You can use the standard's log2 implementation like this:const auto n = 13UL;const auto...

View Article

Answer by user4924658 for What is the fastest/most efficient way to find the...

thats some kind of binary search, it works with all kinds of (unsigned!) integer types#include <climits>#define UINT (unsigned int)#define UINT_BIT (CHAR_BIT*sizeof(UINT))int msb(UINT x){ if(0 ==...

View Article

Answer by jemin for What is the fastest/most efficient way to find the...

The code: // x>=1; unsigned func(unsigned x) { double d = x ; int p= (*reinterpret_cast<long long*>(&d) >> 52) - 1023; printf( "The left-most non zero bit of %d is bit %d\n", x, p);...

View Article
Browsing index pages (73 articles)


Latest Images