Quantcast
Channel: What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C? - Stack Overflow
Browsing latest articles
Browse All 35 View Live
↧

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

For a 4 bit group n, the value can be computed in 3 instructions using a 32bit unsigned value as a lookup table with 16 entries of two bits each:(0xffffaa50 >> (n << 1)) & 3This can be...

View Article


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

I know I am massively late to the party, but I just came across this question and I wanted to share my template solution for getting the highest bit that is set in the argument as a value and not the...

View Article

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

Not too happy with the answers on here, I thought I'd give this one a go myself.Returns 0 if 0 is passed in or index of the bit + 1, for all other values. Handles signed and unsigned values. Is...

View Article

Answer by paperclip optimizer for What is the fastest/most efficient way to...

Since I seemingly have nothing else to do, I dedicated an inordinate amount of time to this problem during the weekend.Without direct hardware support, it SEEMED like it should be possible to do better...

View Article

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

There's a proposal to add bit manipulation functions in C, specifically leading zeros is helpful to find highest bit set. See...

View Article


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

This looks big but works really fast compared to loop thank from bluegsmithint Bit_Find_MSB_Fast(int x2){ long x = x2 & 0x0FFFFFFFFl; long num_even = x & 0xAAAAAAAA; long num_odds = x &...

View Article

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

Here is a fast solution for C that works in GCC and Clang; ready to be copied and pasted.#include <limits.h>unsigned int fls(const unsigned int value){ return (unsigned int)1 <<...

View Article

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

My humble method is very simple:MSB(x) = INT[Log(x) / Log(2)]Translation: The MSB of x is the integer value of (Log of Base x divided by the Log of Base 2).This can easily and quickly be adapted to any...

View Article


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

I assume your question is for an integer (called v below) and not an unsigned integer.int v = 612635685; // whatever value you wishunsigned int get_msb(int v){ int r = 31; // maximum number of...

View Article


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

Another poster provided a lookup-table using a byte-wide lookup. In case you want to eke out a bit more performance (at the cost of 32K of memory instead of just 256 lookup entries) here is a solution...

View Article

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

Woaw, that was many answers. I am not sorry for answering on an old question.int result = 0;//could be a char or int8_t insteadif(value){//this assumes the value is 64bit...

View Article

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

I know this question is very old, but just having implemented an msb() function myself,I found that most solutions presented here and on other websites are not necessarily the most efficient - at least...

View Article

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

Some overly complex answers here. The Debruin technique should only be used when the input is already a power of two, otherwise there's a better way. For a power of 2 input, Debruin is the absolute...

View Article


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

Note that what you are trying to do is calculate the integer log2 of an integer,#include <stdio.h>#include <stdlib.h>unsigned intLog2(unsigned long x){ unsigned long n = x; int bits =...

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


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 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 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 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 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 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 Kaz for What is the fastest/most efficient way to find the highest...

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 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 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 Josh for What is the fastest/most efficient way to find the highest...

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 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 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 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 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 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 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 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 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


What is the fastest/most efficient way to find the highest set bit (msb) in...

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 Evgeny Yashin for What is the fastest/most efficient way to find...

C++ 20: std::bit_width()std::cout << std::bit_width(10u); // prints 4 as of 00001010bNote that it accepts only unsigned types.Deduct 1 to get the zero based position to answer your question.

View Article
Browsing latest articles
Browse All 35 View Live




<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>