I think your example would be a lot more readable if you expressed the constants in binary or hex.

If each result is approximately equally likely, I think it would be more efficient to do a binary chop: a 64-bit input should take no more than 6 tests. For accuracy you should probably write some code to generate this:

my $m = $z > 0x00000000ffffffff ? $z > 0x0000ffffffffffff ? $z > 0x00ffffffffffffff ? $z > 0x0fffffffffffffff ? $z > 0x3fffffffffffffff ? $z > 0x7fffffffffffffff ? 0x8000000000000000 : 0x40000 +00000000000 : $z > 0x1fffffffffffffff ? 0x2000000000000000 : 0x10000 +00000000000 ... etc.

I don't know much about Bloom filters, but it's possible you could construct one to do it faster. Another possibility would be some XS (or Inline C) - some processors have a single instruction to return this, and C compilers often provide functions that use such instructions where available, and fall back to an alternative implementation otherwise. In this case I think you could do it using __builtin_clz() if using gcc.


In reply to Re: Most Significant Set Bit by hv
in thread Most Significant Set Bit by coldr3ality

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.