www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Converting from C

reply =?ISO-8859-1?Q?Julio_C=E9sar_Carrascal_Urquijo?= writes:
How do I convert this C declaration to D:

union FloatIEEE754
{
     float floatValue;
     struct
     {
         unsigned int mantissa : 23;
         unsigned int exponent : 8;
         unsigned int sign : 1;
     } mpn;
};



-- 
Julio César Carrascal Urquijo
http://jcesar.f2o.org/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS$ d- s+:+ a-- C++> ULS++ P++ L+> !E W+++ N+ o? K? w++>
O---  M V? PS+ PE Y+ PGP t+ 5- X+++  R- tv+(++) b++> DI!
D++> G e+> h-- r- y+
------END GEEK CODE BLOCK------
Jun 04 2004
parent Kevin Bealer <Kevin_member pathlink.com> writes:
In article <c9q9ns$14mm$1 digitaldaemon.com>,
=?ISO-8859-1?Q?Julio_C=E9sar_Carrascal_Urquijo?= says...
How do I convert this C declaration to D:

union FloatIEEE754
{
     float floatValue;
     struct
     {
         unsigned int mantissa : 23;
         unsigned int exponent : 8;
         unsigned int sign : 1;
     } mpn;
};
Maybe like this: struct FloatIEEE754 { float floatValue; int as_uint() { return * cast(uint*) (& floatValue); } uint sign() { return as_uint >>> 31; } uint exponent() { return as_uint << 1 >> 24; } uint mantissa() { return as_uint << 9 >>> 9; } } This will allow: struct FloatIEEE754 x; x.floatValue = 1.123; int a = x.sign; int b = x.exponent; int c = x.mantissa; I don't remember how C++ bitfields or IEEE floats are packed, so you may need to reverse the shifts. Kevin
Jun 04 2004