peternilsson42 wrote:
> Thomas Hruska <[EMAIL PROTECTED]> wrote:
>> Using concepts from Safe C++ Design Principles, do:
>>
>> L = (UInt16)((((UInt16)msb) << 8) | (UInt16)lsb);
> 
> I've gotta tell you Thomas, glancing at that, 'safe'
> isn't the first word that comes to mind.
> 
> At the very least, a book on safe design in C++ should not
> be using deprecated C style casts. I can't see how the
> following is any less safe than your version...
> 
>   L = (UInt16(msb) << 8) | lsb;

The "Safe" part was referencing the use of 'UInt16' instead of 
typecasting to 'unsigned int'.  That way the OP can potentially make the 
code work for multiple platforms by changing the typedef for 'UInt16'. 
The C-style casts are one of those die-hard old habits I still have.

You say deprecated but then do UInt16(msb)...isn't that the same thing 
as ((UInt16)msb)?  The actual C++ method is static_cast<>(), but for 
basic types, I find it annoying to type in.


I've been burned so many times by bitwise arithmetic in the past, the 
way I wrote it is the only way I can guarantee (to myself*) the proper 
conversion.

* Yes, it looks ugly but it guarantees every operation occurs within 16 
bits of data.  You probably see the extra conversions as useless.  Fine. 
  It doesn't hurt to have them though.  To me, that helps guarantee to 
_myself_ that the code works.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to