"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:aj******************@newsread1.mlpsca01.us.to .verio.net...
Bryan Parkoff wrote: I tell C++ Compiler to use level 4 warning instead of level 3 warning
so all variables can be calculated accuracy with no prone error.
It will give you a warning like this.
unsigned short A = 0x8002;
unsigned short B = 0x7FFF;
unsigned short C = 0;
C = A + B; // Warning loss high byte conversion -- 0x8002 + 0x7FFF =
0x10002
In fact, A and B are 16 Bits. They go into 32 Bits register to
complete the calculation before they go back to 16 Bits variable in
memory Is it the best practice to add unsigned short () between variable
like this below.
C = unsigned short (A + B); // No Warning
It is like AND to mask only 16 Bits out of 32 Bits. Please advise if
there is a better practice to deal with level 4 warning.
IIRC, this question has been asked and answered a couple of times recently
in 'microsoft.public.vc.language'. I strongly recommend you to post to
a compiler newsgroup about warnings because warnings are compiler-specific
behaviour and are not mandated by the Standard.
V
Victor,
Thank you for the answer. I try to allow my source code to work at most
C++ Compiler, but it does not depend on Microsoft C++ Compiler alone. Intel
C++ Compiler has level 4 warning, but it does not give a warning when large
integer is converted to small integer. Microsoft C++ Compiler does give a
warning.
Please try to answer my question. Is unsigned short (...) the best
practice for C++? Or...Should I use static_cast <...>? Can both unsigned
short (...) and static_cast <...> be ported to most C++ Compilers?
Bryan Parkoff