Hi Maplesoft Support / Community,

I've encountered a critical and bizarre bug involving Bits:-And correctness on large integers (~30 digits) derived from repeated integerdivq2exp operations.

Confirmed Platforms:

  • Maple 2023 (Linux x86_64)
  • Maple 2025 (Linux x86_64)
  • Maple 2025 (Windows x86_64)

Summary:

The correctness of Bits:-And depends on the order of execution

Reproduction:

(See attached common.mpl, bug_test2.mpl, bug_test3.mpl logic).

Case "Fail" (bug_test2.mpl):

  1. Run operation (loops `integerdivq2exp`).
  2. Print result num1 (semicolon).
  3. Define num1_clean (hardcoded same value).
  4. Bits:-And(num1) -> INCORRECT.
  5. Bits:-And(num1_clean) -> INCORRECT.

Case "Pass" (bug_test3.mpl):

  1. Define num1_clean.
  2. Run operation (loops integerdivq2exp).
  3. Bits:-And(num1) -> CORRECT.
  4. Bits:-And(num1_clean) -> CORRECT.

The same behaviour can be observed in Worksheet mode using read.  (See worksheet_driver.mw)

But the result cannot be reproduced if not using read. (See worksheet_version.mw and worksheet_version2.mw)

Code below:

common.mpl

N := 2100:
n := 1000:
num := rand(0 .. 2^N)():
operation := proc(num, n)
    local q, k;
    q := num;
    for k from 1 to 2 do
        q := integerdivq2exp(q, n); 
    end do;
    q;
end proc:

bug_test2.mpl

read "common.mpl";

num1 := operation(num, n);
num1_clean := 1083029963437854242395921050992;

num1_clean_And_result := Bits:-And(num1_clean, integermul2exp(1, n) - 1);
num1_And_result := Bits:-And(num1, integermul2exp(1, n) - 1);

##################################

expected_result := irem(num1_clean, integermul2exp(1, n));

if num1 <> num1_clean then
    error "num1 does not match num1_clean";
end if;
print("num1 matches num1_clean");

if num1_And_result <> num1_clean_And_result then
    error "num1_And_result does not match num1_clean_And_result";
end if;
print("num1_And_result matches num1_clean_And_result");

if num1_And_result <> expected_result then
    error "num1_And_result does not match expected_result";
end if;
print("num1_And_result matches expected_result");

bug_test3.mpl

read "common.mpl";

num1_clean := 1083029963437854242395921050992:
num1 := operation(num, n):

num1_clean_And_result := Bits:-And(num1_clean, integermul2exp(1, n) - 1):
num1_And_result := Bits:-And(num1, integermul2exp(1, n) - 1);

##################################

expected_result := irem(num1_clean, integermul2exp(1, n));

if num1 <> num1_clean then
    error "num1 does not match num1_clean";
end if;
print("num1 matches num1_clean");

if num1_And_result <> num1_clean_And_result then
    error "num1_And_result does not match num1_clean_And_result";
end if;
print("num1_And_result matches num1_clean_And_result");

if num1_And_result <> expected_result then
    error "num1_And_result does not match expected_result";
end if;
print("num1_And_result matches expected_result");

Please Wait...