Solved

Delphi DWORD and/or comparison

Posted on 2009-05-08
5
2,428 Views
Last Modified: 2012-06-21
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
Comment
Question by:cebasso
  • 3
5 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 24340648
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
 
LVL 8

Expert Comment

by:BdLm
ID: 24342632

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
 
LVL 8

Assisted Solution

by:BdLm
BdLm earned 100 total points
ID: 24342641
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
 
LVL 4

Accepted Solution

by:
cebasso earned 0 total points
ID: 24353841
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
 

Expert Comment

by:ypenia
ID: 24382766
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now