By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Level 4 Warning Question

P: n/a
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.

Bryan Parkoff
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a

"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
Jul 23 '05 #3

P: n/a
Bryan Parkoff wrote:
[..]
Please try to answer my question. Is unsigned short (...) the best
practice for C++?
The best? It's not going to compile. For the "functional cast" syntax
you need a _single_word_ type. I'll assume you meant

typedef unsigned short ushort;
....
ushort(...)

And I'd stay away from superlatives. One man's best is another man's
mediocre.
Or...Should I use static_cast <...>? Can both unsigned
short (...) and static_cast <...> be ported to most C++ Compilers?


(assuming the typedef above) Yes, they both are valid. In fact, they do
the same thing: take the expression in parentheses and construct
a temporary of type "unsigned short int" from it. And if you can find
a C++ compiler that doesn't accept either form, I'd be very surprised,
so "to most" should be "to all" C++ compilers.

V
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.