Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2556
  • Last Modified:

Delphi DWORD and/or comparison

Hello!

how to compare dword with AND / OR statment?

example

const
  DWORD_1 = $00060000;
  DWORD_2 = $00F80000;
  DWORD_3 = $00010000;
  DWORD_5 = $0000F000;
  DWORD_6 = $000000FF;
  DWORD_7 = $002000FF;

function CheckDWORD(dwBuff: DWORD): String;
begin
  if (dwBuff or DWORD_1) = DWORD_1 then
  Result := 'DWORD_1,';

  if (dwBuff or DWORD_2) = DWORD_2 then
  Result := Result + 'DWORD_2,';

  if (dwBuff or DWORD_3) = DWORD_3 then
  Result := Result + 'DWORD_3,';

  if (dwBuff or DWORD_4) = DWORD_4 then
  Result := Result + 'DWORD_4,';

  if (dwBuff or DWORD_5) = DWORD_5 then
  Result := Result + 'DWORD_5,';

  if (dwBuff or DWORD_6) = DWORD_6 then
  Result := Result + 'DWORD_6';
end;

var
  dwBuff: DWORD;
begin
  dwBuff := DWORD_3;
  //below OK return only "DWORD_3,"
  ShowMessage(CheckDWORD(dwBuff));
  //but here
  dwBuff := DWORD_2 and DWORD_5;
  ShowMessage(CheckDWORD(dwBuff));
  //return ALL the statments "DWORD_1, DWORD_2, DWORD_3," etc...
  //and was supposed to return only "DWORD2, DWORD5" not ALL :(

How can i do?

Thanks!
Best Regards,
Carlos
0
cebasso
Asked:
cebasso
  • 3
2 Solutions
 
BdLmCommented:
you can split the DWord to 2 x Word and compare word by word like :

procedure TForm1.AndButtonClick(Sender: TObject);
var    x_dw, y_dw  : Dword     ;
       x_hi,x_lo, y_hi, y_lo  : Word;
       highword, lowWord  : Word;
begin

      x_dw := $00060000;
      y_dw := $00060000;
     

 {
      //  high byte      //  lower byte
      x_hi := hi(x_dw);  x_lo := lo(x_dw);
      y_hi := hi(y_dw);  x_lo := lo(y_dw);

      ShowMessage( IntToStr(x_hi)+'.'+ IntToStr(X_lo));
      }


      highword:=x_dw div 65536;
      lowword:=x_dw mod 65536;

      ShowMessage( IntToStr(highword)+'.'+ IntToStr(lowword));

end;
0
 
BdLmCommented:

a bit more, splitting the DWORD ....

      {
      //  high byte      //  lower byte
      x_hi := hi(x_dw);  x_lo := lo(x_dw);
      y_hi := hi(y_dw);  x_lo := lo(y_dw);
 
      ShowMessage( IntToStr(x_hi)+'.'+ IntToStr(X_lo));
      }

Open in new window

0
 
BdLmCommented:
that the complete code >
 
 

unit Unit_dword;
//
//
//
//  have a look at ->  http://www.drbob42.com/uk-bug/hood-07.htm
//  for http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24393689.html#a24342632
//
//
 
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
 
 
  TForm1 = class(TForm)
    AndButton: TButton;
    DWORDANDButton: TButton;
    procedure AndButtonClick(Sender: TObject);
    procedure DWORDANDButtonClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.AndButtonClick(Sender: TObject);
var    x_dw, y_dw  : Dword     ;
       x_hi,x_lo, y_hi, y_lo  : Word;
       highword, lowWord  : Word;
begin

      //  some  options ....
      x_dw := $00060000;
      y_dw := $00060000;

       //  Hight  / Low Word
      highword:=x_dw div 65536;
      lowword:=x_dw mod 65536;
      ShowMessage( IntToStr(highword)+'.'+ IntToStr(lowword));
 
      //  high byte      //  lower byte
      x_hi := hi(x_dw);  x_lo := lo(x_dw);
      y_hi := hi(y_dw);  x_lo := lo(y_dw);
      ShowMessage( IntToStr(x_hi)+'.'+ IntToStr(X_lo));

end;

function DWORDAND (x,y : DWORD) :  DWORD ;
var     x_dw, y_dw  : Dword     ;
        x_hi,x_lo, y_hi, y_lo   : Word;
        highword_x, lowWord_x   : Word;
        highword_y, lowWord_y   : Word;
        highword, lowword       : Word;
begin
       highword_x:=x div 65536;
       lowword_x:=x mod 65536;
       highword_y:=y div 65536;
       lowword_y:=y mod 65536;

       HighWord :=  HighWord_x AND HighWord_y ;
       lowword  := lowword_y  AND lowword_y;

       result := makeLOng ( highword, lowword) ;

end;

procedure TForm1.DWORDANDButtonClick(Sender: TObject);
begin
 
      ShowMessage (IntToStr(DWORDAND($00060000, $00600000)));
end;
end.
0
 
cebassoAuthor Commented:
Hello,
@BdLm
Nice method but doesnt work was i needed...
I found a way...
Peter Below wrote:

"The binary OR operator will combine different values to a result that
has all bits set that are set in any of the values.

You are using the wrong operator. If you want to test if the bits set
in DWORD_1 are also set in dwBuff you have to use the AND operator, not
OR:
if (dwBuff AND DWORD_1) = DWORD_1 then
....
The binary AND operator returns a result that has all bits set that are
set in both operants.
Note that dwBuff can still have other bits set that are 0 in DWORD_1,
the test will still succeed. All you test here is that all bits that
are 1 in DWORD_1 are also 1 in dwBuff.
"
Then the solution is use the OR operator to set the values and AND to compare...
Example
dwBuff := DWORD_1 or DWORD5;
and compare with AND
if (dwBuff and DWORD5 = DWORD_5) then
...
Thanks anyway!
Best Regards,
Carlos
0
 
ypeniaCommented:
maybe this can help
------------------------------------------------------------------------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function CheckDWORD(dwBuff: string): String;

  end;

var
  Form1: TForm1;

const
  DWORD_1 = $00060000;
  DWORD_2 = $00F80000;
  DWORD_3 = $00010000;
  DWORD_5 = $0000F000;
  DWORD_6 = $000000FF;
  DWORD_7 = $002000FF;

implementation

{$R *.dfm}

function TForm1.CheckDWORD(dwBuff: string): String;
var
  sl:TStringList;
  I: Integer;
begin
  sl:=TStringList.Create;
  Result := '';
  sl.CommaText := dwBuff;
  for I := 0 to sl.Count-1 do
  begin
    case StrToInt(sl[i]) of
      DWORD_1:Result := Result + 'DWORD_1,';
      DWORD_2:Result := Result + 'DWORD_2,';
      DWORD_3:Result := Result + 'DWORD_3,';
      DWORD_5:Result := Result + 'DWORD_5,';
      DWORD_6:Result := Result + 'DWORD_6,';
      DWORD_7:Result := Result + 'DWORD_7,';
    end;
  end;
  Result := copy(Result,1,length(Result)-1);
  sl.free;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  s:string;
begin
  s := IntToStr(DWORD_3);
  ShowMessage(CheckDWORD(s));
  s := IntToStr(DWORD_2)+','+IntToStr(DWORD_5);
  ShowMessage(CheckDWORD(s));
end;

end.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now