Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi 3 .  Typecasts

Posted on 1998-05-26
6
Medium Priority
?
1,534 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 total points
ID: 1347927
Yup...here's your answer...Thanks...

Rick
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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

715 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