获取一个字节的高4位

时间:2011-06-26 11:23:18

标签: delphi byte bit bits

我正在尝试获取Byte的高4位。

这是我到目前为止的尝试:

function Upper4Bits(const X : Byte): Byte;
type 
   BS = set of 0..7;
var 
   K : Byte; Q: BS;
begin
  Q := [];
  for K := 0 to 3 do {is it right? upper?}
    {what i need here?}
    Include(Q, {what i put here});

  Upper4Bits := Byte(Q)
end;

提前致谢。

3 个答案:

答案 0 :(得分:8)

根据您对kotlinski答案的评论,您需要result := (byte1 and $F0) or (byte3 and $0F)

答案 1 :(得分:6)

enter image description here

答案 2 :(得分:3)

Upper4Bits := X Shr 4;怎么样?