How use bit/bit-operator to control object state?

橙三吉。 提交于 2019-11-30 21:28:44

I'd really use a set for this. However, I see you really want a byte. Use sets everywhere then typecast to a byte in the end.

This solution will require much less typing, has support for standard delphi operators and really carries no performance penalty as Barry Kelly has pointed out.

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;

I would use a set for this:

type
    TMyDatum = (mdLoaded, mdPersisted, mdChanged, mdMarkedToDelete, ...);
    TMyData = set of TMyDatum;

var
  Foo: TMyData;
begin 
  Foo := [mdLoaded, mdChanged];
  if (mdPersisted in Foo) then ...

These are implemented as integers, so you can pass them easily. And I find the code much, much more readable than bitwise operators.

This page describes Delphi operators, including bitwise operators.

It sounds like you need to use the and operator. For example:

const
  LOADED_FROM_DATABASE = 1;
  PERSISTED = 2;
  CHANGED = 4;
  // etc...

//...

if (bitFlags and LOADED_FROM_DATABASE) <> 0 then
begin
  // handle LOADED FROM DATABASE
end;

if (bitFlags and PERSISTED) <> 0 then
begin
  // handle PERSISTED
end;

// etc...

In order to set the flags, you can use OR:

bitFlags := LOADED_FROM_DATABASE or PERSISTED or CHANGED;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!