能否以bit为单位的结构?就是一个字节分为多个bit来定义?(50分)

  • 主题发起人 主题发起人 whsuperboy
  • 开始时间 开始时间
好象只有变通的方法,没有真正的办法,也许我笨。
 
給幾個例子,僅供參考:
1。取整數的任意一個bit,返回 True(=1), False(0);
function GetBit(A :integer
bit : byte) :boolean;
begin
result := (A and (1 shl bit)) <> 0;
end;

2。取一個字節的高4位与低4位相加

result := (A shr 4) + (A and 15);

另外,用内嵌 assembler 或許是個好主意,可以干很多事:
asm
mov eax, A
mov ebx, 1
shl ebx, bit
xor eax, ebx
...
end
 
搞几个函数不就完了:
function GetBit(X, xBit: Byte): Boolean;
begin
Result:= (X and (1 shl xBit)) <> 0;
end;
procedure SetBit(var X: Byte
xBit: Byte);
begin
X:= X or (1 shl xBit);
end;
procedure ClrBit(var X: Byte
xBit: Byte);
begin
X:= X and ($ff - (1 shl xBit));
end;
procedure SetBitEx(var X: Byte
xBit: Byte
NewVal: Boolean);
begin
if NewVal then SetBit(X, xBit) else ClrBit(X, xBit);
end;
 
多人接受答案了。
 
后退
顶部