位(BIT)操作求教(100分)

  • 主题发起人 Tigerchamp
  • 开始时间
T

Tigerchamp

Unregistered / Unconfirmed
GUEST, unregistred user!
想写一段代码,对一组状态字操作,希望方便地确定每一位是‘1’或‘0’,或置位。想通过字符串进行转换,但觉得可能很笨,我想DELPHI应该会有此类函数,不知哪位大侠可能帮忙。
 
试一试
TBits类,它可以满足你的要求.
一个TBits类可以理解成一个以boole为类型的数组,
很不错.

帮助里有很详细的资料,我想就不用多说了.
连例子都有.
 
delphi有现成的位操作符:not,and,or,xor,shl,shr
我想即使不看帮助也该知道怎么用吧.
 
TBit = 0..31;
{ bit manipulating }
function bitSet(const Value: Int_
const TheBit: TBit): Boolean;
begin
Result:= (Value and (1 shl TheBit)) <> 0;
end;

function bitOn(const Value: Int_
const TheBit: TBit): Int_;
begin
Result := Value or (1 shl TheBit);
end;

function bitOff(const Value: Int_
const TheBit: TBit): Int_;
begin
Result := Value and ((1 shl TheBit) xor $FFFFFFFF);
end;

function bitToggle(const Value: Int_
const TheBit: TBit): Int_;
begin
result := Value xor (1 shl TheBit);
end;

Delphi 本身提供的TBits也不错。
使用方法:
mybits := TBits.Create;
Mybits.Size:=5;
Mybits.bits:=True

 
接受答案了.
 

Similar threads

回复
0
查看
855
不得闲
S
回复
0
查看
950
SUNSTONE的Delphi笔记
S
S
回复
0
查看
774
SUNSTONE的Delphi笔记
S
D
回复
0
查看
740
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部