Reputation:
Provided that I have two bytes variables in Delphi:
var
b1,b2,result:byte;
begin
b1:=$05;
b2:=$04;
result:=??? // $54
end;
How would I then combine the two to produce a byte of value $54
?
Upvotes: 1
Views: 892
Reputation: 76753
The best way would be to:
interface
function combine(a,b: integer): integer; inline; //allows inlining in other units
implementation
function combine(a,b: cardinal): cardinal; inline;
begin
Assert((a <= $f));
Assert((b <= $f));
Result:= a * 16 + b;
end;
Working with byte registers slows down the processor due to partial register stalls.
The asserts will get eliminated in release mode.
If performance matters never use anything but integers (or cardinals).
I have no idea why people are talking about VMT's or dll's. It's a simple inline method that does not even generate a call.
Upvotes: 1
Reputation: 275
The most trivial way is
result := b1 * $10 + b2
"Advanced" way:
result := b1 shl 4 + b2
Upvotes: 3