Solved

Delphi 3 .  Typecasts

Posted on 1998-05-26
6
1,529 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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