gpt4 book ai didi

delphi - 如何使用位/位运算符来控制对象状态?

转载 作者:行者123 更新时间:2023-12-03 15:05:06 24 4
gpt4 key购买 nike

我想创建轻量对象数据包以在客户端和服务器应用程序之间传递。

这是一个非常简单的任务,我只需 1 个字节即可控制,所以一个字节中的每一位都有不同的含义,

仅使用位

0 = False 
1 = True

我现在需要的东西:

1 - Loaded from database 
2 - Persisted
3 - Changed
4 - Marked to Delete
5 -
6 -
7 - Null Value
8 - Read Only


1) How do I use bit operators in Delphi to check each bit value?
2) How do I set the bit Values?

解决方案

在所有帮助之后,我将使用下一组

  TStateType = (
stLoaded = 0, // loaded from persistance
stNative = 2, // value loaded and converted to native type
stPersisted = 3, // saved
stChanged = 4, // object or member changed
stToDelete = 5, // marked to delete
stReadOnly = 6, // read only object, will not allow changes
stNull = 7 // value is null
);
TState = Set of TStateType;

对于流 -> 持久性,这将是要使用的记录:

  TDataPackage = record
Data: TBytes;
TypeInfo: TMetaInfo;
State: Byte;
Instance: TBuffer;
end;

谢谢大家的所有回答和评论。

最佳答案

我真的会为此使用一套。不过,我发现你确实想要一个字节。到处使用集合,然后最后类型转换为字节。

正如 Barry Kelly 指出的那样,该解决方案将需要更少的打字,支持标准的 delphi 运算符,并且确实不会带来性能损失。

procedure Test;
type
TSetValues = (
TSetValue1 = 0,
TSetValue2 = 1,
TSetValue4 = 2,
TSetValue8 = 3,
TSetValue16 = 4,
TSetValue32 = 5,
TSetValue64 = 6,
TSetValue128 = 7
);

TMySet = set of TSetValues;
var
myValue: byte;
mySet: TMySet;
begin
mySet := [TSetValue2, TSetValue16, TSetValue128];
myValue := byte(mySet);
ShowMessage(IntToStr(myValue)); // <-- shows 146
end;

关于delphi - 如何使用位/位运算符来控制对象状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/516646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com