Solved

Delphi 3 .  Typecasts

Posted on 1998-05-26
6
1,528 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IExtractImage Delphi 14 205
Best Firemonkey component pack 1 87
error 1.1 400 Bad request idhttp delphi 18 55
Delphi XE10 Round Image 2 77
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

11 Experts available now in Live!

Get 1:1 Help Now