Beyond De Bruijn: fast binary logarithm of a 10-bit number

Recently I needed a method for retrieving the binary logarithm of a 10-bit number (for the curious, it was for the purpose of converting between 32-bit and 16-bit floating point numbers).

Computing the binary logarithm is equivalent to knowing the position of the highest order set bit. For instance, log2(0x1) is 0 and log2(0x100) is 8.

One well known method for fast binary logarithm is presented at Bit Twiddling Hacks. It is a two-step method where first all lower bits are set to 1 and then a De Bruijn-like sequence is used to perform a table lookup:

int fastlog2(uint32_t v)
    static const int MultiplyDeBruijnBitPosition[32] = 
        0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
        8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31

    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;

    return MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27];

That is 12 integer operations and a table lookup.


It should be obvious what the sequence of operations on v does: fill the integer with ones starting from the highest order bit. Here are a few examples of what happens to v at each step:

v v |= v >> 1 v |= v >> 2 v |= v >> 4 v |= v >> 8 v |= v >> 16
0x0001 0x0001 0x0001 0x0001 0x0001 0x0001
0x0002 0x0003 0x0003 0x0003 0x0003 0x0003
0x0003 0x0003 0x0003 0x0003 0x0003 0x0003
0x0004 0x0006 0x0007 0x0007 0x0007 0x0007
0x0100 0x0180 0x01e0 0x01fe 0x01ff 0x01ff
0x80000000 0xc0000000 0xf0000000 0xff000000 0xffff0000 0xffffffff

There is one obvious optimisation available: since the input is only 10-bit, the last shift operation v |= v >> 16 can be omitted because the final value was already reached.

int fastlog2(uint32_t v)
    static const int MultiplyDeBruijnBitPosition[32] = 
        0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
        8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31

    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;

    return MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27];

10 instructions instead of 12. Not really amazing, but worth mentioning.

Optimising more?

Could we do better? Now the last line is v |= v >> 8; and it is only useful to propagate the 9th and 10th bits to positions 1 and 2. What happens if we omit that line? Let’s see:

  • For most values of v, the expected value is obtained.
  • For values of v with a highest order bit at 9th position, 0x1fe could be obtained instead of 0x1ff.
  • For values of v with a highest order bit at 10th position, one of 0x3fc, 0x3fd or 0x3fe could be obtained instead of 0x3ff.

The list of possible output values would therefore be 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1fe, 0x1ff, 0x3fc, 0x3fd, 0x3fe, 0x3ff. What happens to these values when multiplying them with the De Bruijn sequence? Let's see:

v v * 0x07C4ACDDU) >> 27
0x1 0
0x3 2
0x7 6
0xf 14
0x1f 30
0x3f 29
0x7f 27
0xff 23
0x1fe 15
0x1ff 16
0x3fc 30
0x3fd 31
0x3fe 0
0x3ff 1

Damn! Two values are colliding. It looks like we cannot omit the last line after all.

Beyond De Bruijn

Let’s give another try at the problem. Usually De Bruijn sequences are built using either nontrivial algorithms, or brute force. Maybe we could find another sequence that has no collision? Or a sequence that is not a De Bruijn sequence but that works for our problem?

Well, let’s just brute force!

(2 seconds later)

int fastlog2(uint32_t v)
    static const int MagicTable[16] = 
        0, 1, 2, 8, -1, 3, 5, 9, 9, 7, 4, -1, 6, -1, -1, -1

    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;

    return MagicTable[(uint32_t)(v * 0x5a1a1a2u) >> 28];

Down to 8 instructions instead of 12. And the lookup table is now half the size!


It is possible for multiply-and-shift techniques similar to the De Bruijn sequence algorithm to exist for a larger set of problems. Brute forcing the search is a totally valid method for 32-bit multiplication.

The code used for this article is included in the attached file.

  • Posted: 2012-04-03 23:15 (Updated: 2012-04-04 10:35)
  • Author: sam
  • Categories: optim c code c++

Attachments (1)

Download all attachments as: .zip


1. anonymous -- 2012-05-26 05:30

Not to spoil the fun, but bsr can do what you are doing much faster. GCC even has a builtin for it namely, builtin_clz.

2. sam -- 2012-05-27 13:32

@anonymous: Yes, you are right. I hope you will agree to the pedagogical value of the example nonetheless.

3. jilles -- 2012-11-11 16:59

Actually, on Intel Atom and AMD K8, BSR is very slow and the example may be faster in inner loops. However, even on such processors, BSR may be better because less cache space (instruction as well as data) is used.

325. subanal -- 2016-11-27 07:00

iUQr2d Thank you, I ave been seeking for details about this subject for ages and yours is the best I ave found so far.

394. XRumerTest -- 2017-04-01 14:51

Hello. And Bye.

398. XRumerTest -- 2017-05-08 04:44

Hello. And Bye.

399. lucy ann -- 2017-05-09 15:32

NYZj6K Really informative blog article. Really Cool.

400. Jessica Barden -- 2017-11-13 14:27

I am doing a lengthy research on binary logarithm via "UK Essay Help": Your write up helped me in gathering information.

401. anonymous -- 2017-11-28 07:33

We should give another attempt to the issue. As a rule, De Bruijn successions are constructed utilizing either nontrivial calculations or savage power. Possibly we could discover another arrangement that has no impact? Or, on the other hand, a succession that isn't a De Bruijn grouping however that works for our concern?

Web Source:

402. anonymous -- 2017-11-28 12:26

You join the latest free online mozilla firefox free download latest version and i sure you create the best speed to downloading just click here most people excited to join the free firefox download online you upgrade your old browser and have the best browsing.

403. anonymous -- 2018-01-13 06:38

<a href="">test</a>

404. Mike -- 2018-01-13 06:42

I have read the post, you have posted very nice tutorial which i like reading. I have got information which i did not know before. I will come back to read more similar articles. Buy sticker roll

405. Ramon -- 2018-01-13 08:11

This is a great educational post. I am impressed to see the article you have posted really found it very good to read. I have learned a lot which i did not know before. Buy sticker printing from

406. Andre -- 2018-02-05 21:37

I almost went crazy with so many numbers, but I understood the explanation hahaha

407. Amanda Johnson -- 2018-03-03 02:34

You should check different traps to register the log2 and consider utilizing the MSR get together direction on the off chance that you are on x86(_64). It gives you the record of the most noteworthy set piece, which is precisely what you require.

UK Dissertation Help

408. anonymous -- 2018-03-20 13:34

What a amazing post shared here. I really love this website, I would like to say thanks for sharing such great post. I would like to say bundle of thanks for sharing. C9060-528

409. R Programming Assignment Assistance -- 2018-03-26 12:49

I personally like your post, you have shared good article. It will help me in great deal. <a href="">R Programming Assignment Assistance</a>

410. Statistics Homework Help -- 2018-03-26 12:50

This is really great work. Thank you for sharing such a useful information here in the blog. <a href="">Statistics Homework Help</a>

411. Operations Research Assignments help -- 2018-03-26 13:45

This was a great and interesting article to read. I have really enjoyed all of this very cool information

412. Sociology Assignment Help -- 2018-03-26 13:46

My friend recommended this blog and he was totally right keep up the good work

413. anonymous -- 2018-05-10 20:26

I am really poor in mathematics but, after started following your website I learned a lot about math subject. The tricks which you have posted in custom writing service reviews blog have made me to visit your blog. After visiting your blog I came to know the beauty of this subject and learned tricks to solving problems.

414. Scott Stevens -- 2018-05-17 11:18

This is an extraordinary instructive post. I am awed to see the article you have posted extremely thought that it was great to peruse. I have taken in a great deal which I didn't know previously.<a href="">Cheap Essay Writing Service UK</a>

415. anonymous -- 2018-06-30 13:25

Excellent article. Very interesting to read. I really love to read such a nice article.

416. Francesca Godfrey -- 2018-07-02 12:49

If you had been hoping for an elegant mathematical formulation to describe them or theorem to provide them or something comparable, I suppose this would require profound insight into range concept and possibly different fields that are beyond my skillset. If I wherein to make a wild wager I would guess they may be produced by means of cellular automata. This isn't always an answer why? On a rigorous base, however, an attempt to intuitively recognize why it really works and why it works nicely so that you can use it with self-belief. Essay Writing Service UK

417. anonymous -- 2018-07-06 07:00

Students often feel issues in writing assignment, so to make students comfortable in java assignment we help them to write their assignments. If ever you feel any problem in writing something you can visit our website Java Assignment Help.If you want help in writing assignment than contact our experts.

418. anonymous -- 2018-07-11 12:00

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any suggestions? Many thanks! como conquistar o ex

419. anonymous -- 2018-07-12 14:03

I've had a mental obstacle for a decent 5 years and this was the principal thing that started my imagination in, well, five years lol. I quit composing 500 pages into a novel (that will require a considerable measure of altering lol) that is a piece of a set of three I have sketched out in detail. Actually the peak of the book, and I just.... couldn't continue onward. I compose a considerable measure for school/work yet not in an inventive sense.

420. anonymous -- 2018-07-12 14:14

Impressive web site, Distinguished feedback that I can tackle. I am moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards.

421. anonymous -- 2018-07-12 14:14

Impressive web site, Distinguished feedback that I can tackle. I am moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. <a href=''></a>

422. anonymous -- 2018-07-12 14:15

Impressive web site, Distinguished feedback that I can tackle. I am moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards.

423. anonymous -- 2018-07-14 14:18

This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it <a href=''>Subi Cevac</a>

424. anonymous -- 2018-07-14 14:18

This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it Subi Cevac

425. anonymous -- 2018-07-15 12:55

I have bookmarked your blog, the articles are way better than other similar blogs.. thanks for a great blog! <a href=''>a course in miracles</a>

426. anonymous -- 2018-07-16 13:48

Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. a course in miracles

427. aD -- 2018-07-18 07:54

The information you have posted is very useful. The sites you have referred was good. Thanks for sharing..

428. anonymous -- 2018-07-18 13:41

I would like to say that this blog really convinced me to do it! Thanks, very good post. <a href=''>ucdm</a>

429. William -- 2018-07-19 10:19

Custom Deluxe Boxes specialize in designing custom product boxes of all styles, sizes and materials. We are known as the manufactures of best custom product boxes in USA and Canada. Our team has gained extensive experience by working for different manufacturers, companies and brands. We have helped thousands of our clients to be successful by giving the right look to their product packaging.

430. anonymous -- 2018-07-20 09:34

Thanks for this great post, i find it very interesting and very well thought out and put together. I look forward to reading your work in the future. click this

431. anonymous -- 2018-07-22 06:53

The information you have posted is very useful. The sites you have referred was good. Thanks for sharing... <a href=''>browse this site</a>

432. anonymous -- 2018-07-22 09:11

Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on, and I have been at the look out for such information. discover here

433. anonymous -- 2018-07-24 07:27

Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. Lebensversicherung auflösen

434. anonymous -- 2018-07-28 12:51

useful information on topics that plenty are interested on for this wonderful post.Admiring the time and effort you put into your b!.. Mon Bazar

435. anonymous -- 2018-07-29 09:18

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. Ottawa Distribution Services

436. anonymous -- 2018-07-29 15:33

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! Sharrard Law

437. anonymous -- 2018-07-30 12:11

All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks a course in miracles

438. anonymous -- 2018-07-31 13:53

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. a course in miracles

439. anonymous -- 2018-08-02 12:07

I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!.. Primeworld District Lapu Lapu

440. anonymous -- 2018-08-05 12:11

To score good marks in their college and university, they can avail our excellent services. Our assignment experts can write complicated <a href="">Online Assignment Help</a> as they are well-versed with every academic topic.

441. anonymous -- 2018-08-06 17:56

I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. Transfer Super 8 Film to DVD

442. -- 2018-08-11 11:22

If students are getting knowledge in the Singaporean university and want to get a higher score in your assignment then worry no more, come at and take our Assignment Help Singapore services.

Check Here:

443. anonymous -- 2018-08-19 18:10

Your blog website provided us with useful information to execute with. Each & every recommendations of your website are awesome. Thanks a lot for talking about. time bazar chart

444. -- 2018-09-14 14:25

Hi i am from candle boxes wholesale Dodo Packaging provides high-quality printing solution to their customers. Provide top quality wholesale custom boxes with packaging. Free design and shipping all over the world with 24/7 customer support.

custom boxes with logo

445. alyssacaroline -- 2018-10-03 11:04

Achievement in administration requires learning as quick as the world is evolving. I have discovered that as long as I hold quick to my convictions and qualities - and pursue my own ethical compass then the main desires I have to satisfy are my own. I have data which I didn't know previously. I will return to peruse more comparable articles.

446. anonymous -- 2018-10-05 12:01

Je vous remercie de l'information! Je cherchais et ne pouvait pas trouver. Vous me aidé!

447. anonymous -- 2018-10-19 19:23

I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all <a href="">More Details</a>

448. anonymous -- 2018-10-21 17:55
449. anonymous -- 2018-11-05 06:44

Hey really such a amazing post dear.

450. -- 2018-11-19 07:39

United Parcel Service, Inc. is a logistics and package delivery company, which provides supply chain management services.

451. anonymous -- 2018-11-22 17:09

Thanks for sharing your thoughts and ideas on this one. Please keep posting about such articles as they really spread useful information. Thanks for this particular sharing. I hope it stays updated.

452. Jasmine Freeman -- 2018-11-23 08:06

Students find it tough to write and submit their essay papers on time. And in those stressful conditions, they ask different sources, Academic Writing Services UK . Fortunately, with our online writing company, all they need is to discuss their writing requirements with us where we deliver the top quality content at a very quick pace.

453. anonymous -- 2018-11-26 12:48

Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. look at more info

454. Mickie -- 2018-11-26 14:14
455. anonymous -- 2018-11-27 09:17

Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging, click here

456. anonymous -- 2018-11-29 07:36

This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. JB Records

457. anonymous -- 2018-12-05 02:41

Keep up the excellent work !! Lovin'

458. anonymous -- 2018-12-09 05:49

This blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. Max Beltoon

459. anonymous -- 2018-12-09 11:35

nice post, keep up with this interesting work. It really is good to know that this topic is being covered also on this web site so cheers for taking time to discuss this! Ichbin Christ

460. anonymous -- 2018-12-10 12:59

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work Explore Capitola

461. anonymous -- 2018-12-26 08:00

please use this website.

462. anonymous -- 2019-01-08 11:27

Thank you for such a well written article. It’s full of insightful information and entertaining descriptions. Your point of view is the best among many. Digital X-ray Solution

463. kaizen -- 2019-01-10 05:46

I regularly visit your site and find a lot of interesting information. Not only good posts but also great comments. Thank you and look forward to your page growing stronger.

464. anonymous -- 2019-01-10 10:13

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! Wordzilla

465. anonymous -- 2019-01-18 13:04

Thanks for sharing this. keep sharing with us.

466. katyciarawriter -- 2019-01-24 02:17

Thanks for sharing your thoughts and thoughts in this one. Please preserve posting approximately such articles as they sincerely unfold beneficial data. Thanks for this unique sharing. I wish it stays up to date. Real Dissertation Help Online

467. katedaisy -- 2019-01-24 03:15

I was checking continuously this blog and I'm impressed! Very useful information particularly the last part, I care for such info a lot. router login

468. beatrixkohn -- 2019-02-02 07:57

If you want a custom essay that is written professionally and can help you to score good marks, we can help you. Our essays assist you in making a good impression on the teacher.

469. anonymous -- 2019-02-12 05:59

I have read a lot of articles and this is the most satisfactory article. It has very well exploited the problem.

470. Alexandre -- 2019-02-18 10:48

When a student intends to buy a research paper from our online custom writing service; he is entitled to get a well researched and quality oriented paper with all his instructions

Add New Comment