Level 4 Warning Question

B

Bryan Parkoff

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
 
V

Victor Bazarov

Bryan said:
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
 
B

Bryan Parkoff

Victor Bazarov said:
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
 
V

Victor Bazarov

Bryan said:
[..]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top