简体   繁体   English

在delphi中合并两个字节

[英]Combine two bytes in delphi

Provided that I have two bytes variables in Delphi: 前提是我在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 ? 然后,我将如何结合两者以产生值$54的字节?

The most trivial way is 最简单的方法是

result := b1 * $10 + b2

"Advanced" way: “高级”方式:

result := b1 shl 4 + b2

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. 我不知道为什么人们在谈论VMT或dll。 It's a simple inline method that does not even generate a call. 这是一个简单的内联方法,甚至不会生成调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM