value range propagation for _bitwise_ OR

Ali Çehreli acehreli at yahoo.com
Sun Apr 11 12:16:34 PDT 2010


Rainer Deyke wrote:

 > The intention of fill_bits is to create a number that contains all of
 > the bits of all of the numbers from min_v to max_v.

But no value in the range may have all those bits set. As a result, a|b 
may have less bits set than fill_bits returns.

 > In other words:
 >
 > min_v | (min_v + 1) | (min_v + 2) | ... | (max_v - 1) | max_v

That's the problem: Only one of those values is used in a|b at a time.

Here is the test code that I've been using when I tried to solve this 
problem:

import std.random;
import std.stdio;

void report(string label, uint value)
{
     writefln("%25s %032b %08x %10s", label, value, value, value);
}

// ...

unittest
{
     foreach (i; 0 .. 1000) {

         uint max_a = uniform(0, 1024);
         uint min_a = uniform(0, max_a + 1);

         uint max_b = uniform(0, 1024);
         uint min_b = uniform(0, max_b + 1);

         uint calculated_max_c = max_c_Deyke_2(min_a, max_a, min_b, max_b);

         report("min_a", min_a);
         report("max_a", max_a);
         report("min_b", min_b);
         report("max_b", max_b);
         report("calculated", calculated_max_c);

         uint emp_max_a;
         uint emp_max_b;
         uint empirical_max_c;
         foreach (a; min_a .. max_a + 1) {
             foreach(b; min_b .. max_b + 1) {
                 if ((a | b) > empirical_max_c) {
                     emp_max_a = a;
                     emp_max_b = b;
                     empirical_max_c = a | b;
                 }
             }
         }

         if (empirical_max_c != calculated_max_c) {
             report("WRONG! empirical", empirical_max_c);
             report("emp_max_a", emp_max_a);
             report("emp_max_b", emp_max_b);
             break;
         }
     }
}

Yes, it's incomplete, but does catch problems. :) So far, Steven 
Schveighoffer's maxor() is the only one that passes the tests. (There 
may be other solutions that I've missed.)

Ali



More information about the Digitalmars-d mailing list