Solved

Delphi DWORD and/or comparison

Posted on 2009-05-08
5
2,493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month7 days, 13 hours left to enroll

632 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