Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary operations.

Status
Not open for further replies.

Nordlund

Programmer
Jul 17, 2001
458
SE
Hi.
I'm too lazy to figure it out by myself.

How do I perform binary operations in D7?

For example (this doesn't work):
Code:
function DoSomething(y: longword): longword
begin
  Result := y AND $FF00;    
end;

KungTure-RX.jpg

//Nordlund
 
???

Did a small test (in D7), worked fine for me..

Code:
function DoSomething(y: cardinal): cardinal;
begin
  Result := y AND $FF00;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 showmessage(format('0x%x',[ dosomething($1234)]));
end;

buttonclick shows '0x1200' which is correct...

--------------------------------------
What You See Is What You Get
 
Hi.
That's the weird thing.
It !Should! work, but it doesn't....

I solved by using assebler code instead (Code example may not work properly):

Code:
function DoSomething(y: cardinal): cardinal;
begin
  asm
    mov eax,y
    and eax,$FF
    mov Result, eax
  end;
end;

KungTure-RX.jpg

//Nordlund
 
what do you mean by 'it doesnt work' ??
does it give you a bogus result?

have you checked your compiler settings??
...
??

--------------------------------------
What You See Is What You Get
 
It gives me the result of 0 every time...

well, it's solved, but thanks anyway...

KungTure-RX.jpg

//Nordlund
 
Your Assembler code is not the equivalent to your Delphi Pascal code. Is that the cause of the problem?

What values of y did you use and what were the answers?

Which version of Delphi did you use?

Andrew
Hampshire, UK
 
OK. Here is the thing.
I'm trying to convert a IP address stored in a LongWord to a IP address splitted in 4 integers.

I tried to mask out the highest 8 bits of the longword, then divide it to get a 0-255 number, and so on...


I might have "reinvented" the wheel by writing own code here :)

By the way, I'm using Delphi 7...

KungTure-RX.jpg

//Nordlund
 
Hi.
When I tried the same example, I got the result I wanted.
It must have been SBK (Shit behind the Keyboard) or less likely, compiler error...


I tried this (exact code) and it didn't work:
Code:
  Edit1.Text := FloatToStr(($CA7AB569 AND $FF000000) div $1000000);

Well, this code does the trick too:
Code:
procedure DecimalToIP(const IP: LongWord; var IP1, IP2, IP3, IP4: Integer);
begin
  asm
    // IP Highest pair
    mov eax,IP
    shr eax,24
    and eax,$FF
    mov IP1, eax

    // IP "almost" highest pair
    mov eax,IP
    shr eax,16
    and eax,$FF
    mov IP2, eax

    // IP "almost" lowest pair
    mov eax,IP
    shr eax,8
    and eax,$FF
    mov IP3, eax

    // IP lowest pair
    mov eax,IP
    and eax,$FF
    mov IP4, eax
  end;
end;


Sorry for wasting you time guys! :)

KungTure-RX.jpg

//Nordlund
 
If you want to avoid assember then you might find this useful:
Code:
procedure DecimalToIP(const IP: LongWord; var IP1, IP2, IP3, IP4: Integer);
begin
  ip1 := ( ip and $FF000000 ) shr 24;
  ip2 := ( ip and $00FF0000 ) shr 16;
  ip3 := ( ip and $0000FF00 ) shr 8;
  ip4 := ( ip and $000000FF );
end;

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top