Solved

Delphi 3 .  Typecasts

Posted on 1998-05-26
6
1,530 Views
Last Modified: 2008-03-17
I need to manipulate bits within a single. Under D1 I did it like this...
Delphi won't allow bitwise operations on reals (why not!!) so the single is treated as a longint to do the manipulations, then the resulting bit pattern is treated as a single.
Type
    SType = single;
    LType = longint;
Var
    sing1 : single;
    long1 : longint;

long1 := LType(sing1);  {typecast the single as a longint}

{do the SHR, AND, OR manipulations here}

sing1 := SType(long1);  {longint back to a single}

Under D3 I get a compiler error "invalid typecast".
Can anyone tell me why, and how do I fix it.

Thanks.
0
Comment
Question by:frog
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:Socrates050697
ID: 1347922
Instead of typecasting, use the ROUND() function to convert the single to an integer. Also  you don't need to typecast to go the other way.

eg


    Var
        sing1 : single;
        long1 : longint;

    long1 := Round(sing1);

    {do the SHR, AND, OR manipulations here}

    sing1 := long1;



Socrates
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1347923
Okay...you can't do it directly but you can do it indirectly...pass the address back and forth and manipulate the values of the addresses...

type
  pMyLongInt = ^Integer;
  pMySingle = ^Single;
var
  mySingle: single;
  myLongInt: LongInt;
  ptrMyLongInt : pMyLongInt;
  ptrMySingle: pMySingle;
  myPointer: Pointer;
begin

  mySingle := 2.01;
  myPointer := @mySingle;
  ptrmyLongInt := myPointer;
  myLongInt := ptrmyLongInt^;
  myLongInt := myLongInt SHL 1;
  myPointer := @myLongInt;
  ptrMySingle := myPointer;
  mySingle := ptrMySingle^;
  Label1.Caption := FloatToStr(mySingle);

0
 
LVL 3

Expert Comment

by:rickpet
ID: 1347924
In Delphi 1 you need to change my Type of pMyLongInt to
pMyLongInt = ^LongInt (In 32bit world LongInt = Integer)

Rick
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:frog
ID: 1347925
Socrates, thanks for your reply but the round() will convert the real to a longint value and data format, it therefore will not preserve the bit pattern of the single which I want to manipulate.

rickpet, thanks for the pointers reminder. I'm sure it will work, I'll do it when I get time.


0
 

Author Comment

by:frog
ID: 1347926
rickpet I'll accept your answer but I don't think there is any way for me to give you the points until you send it as an answer instead of a comment. Is that right?

0
 
LVL 3

Accepted Solution

by:
rickpet earned 100 total points
ID: 1347927
Yup...here's your answer...Thanks...

Rick
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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