June 04, 2004 Converting from C | ||||
---|---|---|---|---|
| ||||
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------
|
June 04, 2004 Re: Converting from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio César Carrascal Urquijo | 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 |
Copyright © 1999-2021 by the D Language Foundation