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

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


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

One more way to achieve this using the same idea of branchless binary search has for future reference.This iteration seems to unroll on most compilers and compile options I've been testing on Godbolt...

View Article
Browsing all 36 articles
Browse latest View live


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