473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Level 4 Warning Question

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
3 1977
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul Rubin | last post by:
That's what the Python style guides advise. They don't seem to like def frob(x): import re if re.search('sdfxyz', x): ... instead preferring that you pollute your module's global namespace...
3
by: Maciej Pilichowski | last post by:
Hello there, The short version -- how to set warning level just for one specific file, or more exactly how to relax warning level for it? The long version -- I try to work at maximum warning...
0
by: Aaron | last post by:
I've been using Access 2003 databases for a while. Suddenly, yesterday, whenever my users open a database, they get a warning message saying something about "block unsafe expressions", and then,...
43
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It...
6
by: J.D. Herron via .NET 247 | last post by:
Just wanted to throw out there a VC7.1 compiler bug when usingwarning level 4. The following code will demonstrate theproblem: class TestClass { public: // Initialized float member,...
2
by: Kathy | last post by:
One of my customers is running an Access 2003 application. They have the Macro Security Level set to Low. When the .mde app is executed directly from the C: drive on their server, they get into...
0
by: B. | last post by:
We just recently move to code from VC++6 to VC++.NET 2005 and upgrade the warning level from 3 to 4. In debug mode, we compile the application with no warning no error, but when I build it in...
6
by: Maxim Veksler | last post by:
Hello, I wish to do some low level network stuff using python. I've googled somewhat and came up with pylibpcap, trouble is I can't compile it on my Ubuntu 6.10 workstation. Can someone please...
0
by: Paul Jones | last post by:
Hi, Can anyone tell me how to set the complier warning level used when compling c# code for Visual Studio 2005 web sites? I have found various references on the net suggesting a change to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.