The calculation of "Mod_" in procedure "DivMod" gives a wrong result in "Mod_" (should be 272) and spoils at the same time the content of variable "Div_" that was correct just before.
See comment in the source code for details.
Thanks in advance!
Code: Select all
program PowersOf2;
{ Declarations section }
var Value_, Tmp1, Res1, Res2: Dword;
function PowerOf(Value: DWord): word;
var Tmp: word;
begin
Tmp := 0;
while Dword(1 shl Tmp) < Value do inc(Tmp);
Result := Tmp;
end;
procedure DivMod(Val_, Divider: DWord; var Div_, Mod_: Dword);
var Power: word;
begin
Power := PowerOf(Divider); // Power = 9 as expected
Div_ := Val_ shr Power; // Div_ is 19 as expected
Mod_ := Div_ shl Power; // Mod_ is 9728 as expected
Mod_ := Val_ - Mod_; // <----- here it goes wrong, >both< var params "Div_" and "Mod_" are messed up
end;
begin
{ Main program }
Value_ := 512;
Tmp1 := 10000;
DivMod(Tmp1, Value_, Res1, Res2);
end.