473,883 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Left shift

Hi,

in the header of my class I've a constant

static const int a = ( 1 << 32 ) - 1;

When compiling the code, g++ issues the warning
"warning: left shift count >= width of type"

Why? And how can I get rid of that?

Thank you.
Chris
Aug 23 '06
56 15369
In article <ec**********@b lue.rahul.net>, sp*****@speedym ail.org says...
Jerry Coffin <jc*****@taeus. comwrote:
>sp*****@speedym ail.org says...
Do you agree or disagree that a constant expression being
assigned to a pointer must be evaluated at compile time?
If you think it's necessarily true, please point to part of
the standard that says or at least implies that it should be.

Section 4: "An expression e can be implicitly converted to
a type T if and only if the declaration T t = e is well-formed".
That doesn't mention anything about "compile time".

[ ... ]
However, one could argue that evaluating the constant expression at
runtime and issuing a runtime error ("dynamic type mismatch", or
some such) would be a correct implementation.
One certainly could. I believe one would have exceptional difficulty
finding anything to the contrary in the standard.
I'm not sure if that's a valid argument, but it's certainly an
unattractive solution.
I'm pretty sure it is a valid argument. IMO, when C++ is implemented as
a compiler, it's better for as many errors as possible to prevent
compilation, rather than resulting in runtime errors. That, however, is
a personal opinion about quality of implementation -- while I'm fairly
sure some parts of the standard were written to allow and perhaps even
subtly encourage it, I don't believe it's a requirement.

As far as being unattractive: it depends. I can think of some fair
reasons for using a C++ interpreter, and an interpreter more or less
eliminates the whole concept of "compile time"...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 29 '06 #41
Jerry Coffin wrote:
In article <ec**********@b lue.rahul.net>, sp*****@speedym ail.org says...
>Jerry Coffin <jc*****@taeus. comwrote:
>>sp*****@speedym ail.org says...
>Do you agree or disagree that a constant expression being
assigned to a pointer must be evaluated at compile time?
If you think it's necessarily true, please point to part of
the standard that says or at least implies that it should be.

Section 4: "An expression e can be implicitly converted to
a type T if and only if the declaration T t = e is well-formed".

That doesn't mention anything about "compile time".

[ ... ]
>However, one could argue that evaluating the constant expression at
runtime and issuing a runtime error ("dynamic type mismatch", or
some such) would be a correct implementation.

One certainly could. I believe one would have exceptional difficulty
finding anything to the contrary in the standard.
>I'm not sure if that's a valid argument, but it's certainly an
unattractive solution.

I'm pretty sure it is a valid argument. IMO, when C++ is implemented as
a compiler, it's better for as many errors as possible to prevent
compilation, rather than resulting in runtime errors. That, however, is
a personal opinion about quality of implementation -- while I'm fairly
sure some parts of the standard were written to allow and perhaps even
subtly encourage it, I don't believe it's a requirement.
Hm, I am not sure. [1.4/2] says:

Although this International Standard states only requirements on C++
implementations , those requirements are often easier to understand if they
are phrased as requirements on programs, parts of programs, or execution
of programs. Such requirements have the following meaning:

? If a program contains no violations of the rules in this International
Standard, a conforming implementation shall, within its resource limits,
accept and correctly execute3) that program.
? If a program contains a violation of any diagnosable rule, a conforming
implementation shall issue at least one diagnostic message, except that
? If a program contains a violation of a rule for which no diagnostic is
required, this International Standard places no requirement on
implementations with respect to that program.

Now, I would maintain that it is the "implementation " which is required to
issue a diagnostic message of a diagnosable rule and not the program during
its execution. Thus, if the implementation is a compiler (so that it is
observable whether the implementation or the program issues the message),
diagnostics for diagnosable rules have to be issued by the compiler. At
least that is how I read the second item in the list.

In particular: if I compile a program but never run it, a successful compile
without diagnostics should mean that the program satisfies all diagnosable
rules for which diagnostics are not explicitly waived.
As far as being unattractive: it depends. I can think of some fair
reasons for using a C++ interpreter, and an interpreter more or less
eliminates the whole concept of "compile time"...
True, and in that case, you cannot really observe whether the implementation
or the program issues the diagnostics. However, the standard says it should
be the implementation.
Best

Kai-Uwe Bux
Aug 29 '06 #42
In article <ed**********@m urdoch.acc.Virg inia.EDU>, jk********@gmx. net
says...

[ ... diagnosing at compile time rather than execution time: ]
Hm, I am not sure. [1.4/2] says:
[ ... ]
? If a program contains no violations of the rules in this International
Standard, a conforming implementation shall, within its resource limits,
accept and correctly execute3) that program.
[ ... ]
Now, I would maintain that it is the "implementation " which is required to
issue a diagnostic message of a diagnosable rule and not the program during
its execution.
I've re-quoted part of your quote from the standard above. This makes it
quite clear that it is the implementation that executes the program --
as such, things that are part of the implementation can quite reasonably
happen at execution time.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 29 '06 #43
Jerry Coffin <jc*****@taeus. comwrote:
sp*****@speedym ail.org says...
>Section 4: "An expression e can be implicitly converted to
a type T if and only if the declaration T t = e is well-formed".
>That doesn't mention anything about "compile time".
>However, one could argue that evaluating the constant expression at
runtime and issuing a runtime error ("dynamic type mismatch", or
some such) would be a correct implementation.
>One certainly could. I believe one would have exceptional difficulty
finding anything to the contrary in the standard.
>I'm not sure if that's a valid argument, but it's certainly an
unattractive solution.
I'm pretty sure it is a valid argument.
Okay, I now agree with your position -- the constant evaluation
can occur at runtime if a resulting type mismatch causes a
runtime error.

Had you said this in the first place, rather than making
misleading arguments about undefined behavior that completely
missed the point, we would have been in agreement far sooner.

Steve
Aug 29 '06 #44
In article <ed**********@b lue.rahul.net>, sp*****@speedym ail.org says...

[ ... ]
Had you said this in the first place, rather than making
misleading arguments about undefined behavior that completely
missed the point, we would have been in agreement far sooner.
I certainly didn't intend anything I said to be misleading.

The original piece of code included undefined behavior -- and I'm
reasonably convinced that undefined behavior "trumps" nearly anything
else -- undefined behavior removes the requirement for a diagnostic
(along with all other requirements).

The more recent bit of code you posted did not include undefined
behavior -- and in its absence, I'm reasonably certain a diagnostic is
required.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 29 '06 #45
Jerry Coffin wrote:
In article <ed**********@m urdoch.acc.Virg inia.EDU>, jk********@gmx. net
says...

[ ... diagnosing at compile time rather than execution time: ]
>Hm, I am not sure. [1.4/2] says:

[ ... ]
> ? If a program contains no violations of the rules in this
International
Standard, a conforming implementation shall, within its resource
limits, accept and correctly execute3) that program.

[ ... ]
>Now, I would maintain that it is the "implementation " which is required
to issue a diagnostic message of a diagnosable rule and not the program
during its execution.

I've re-quoted part of your quote from the standard above. This makes it
quite clear that it is the implementation that executes the program --
as such, things that are part of the implementation can quite reasonably
happen at execution time.
The sentence that you emphasized only deals with programs that contain no
violations of any diagnosable rules and only requires that those programs
be executed. The sentence does not apply at all to programs that violate
any diagnosable rule and neither gives any license nor any requirement for
what an implementation is supposed to do about those programs.

Now, as far as the execution of programs that violate rules is concerned,
you will find relevant language in [1.4/8]:

A conforming implementation may have extensions (including additional
library functions), provided they do not alter the behavior of any
well-formed program. Implementations are required to diagnose programs
that use such extensions that are ill-formed according to this
International Standard. Having done so, however, they can compile and
execute such programs.

Note that the standard demands that the diagnostics are given *before* the
program is executed ("Having done so").

Thus, although

int* p;
p = 1;

is ill-formed an implementation may take the license to compile and execute
a program containing those line (as an extension), but it is required to
flag the error beforehand.
Best

Kai-Uwe Bux
Aug 29 '06 #46
In article <ed**********@m urdoch.acc.Virg inia.EDU>, jk********@gmx. net
says...

[ ... ]
A conforming implementation may have extensions (including additional
library functions), provided they do not alter the behavior of any
well-formed program. Implementations are required to diagnose programs
that use such extensions that are ill-formed according to this
International Standard. Having done so, however, they can compile and
execute such programs.

Note that the standard demands that the diagnostics are given *before* the
program is executed ("Having done so").
By this reading, it's also required to issue the diagnostic *before*
compiling the program. Given that it's normally the compiler the issues
such diagnostics, I think you're reading more into it than is really
there...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 29 '06 #47
Jerry Coffin wrote:
In article <ed**********@m urdoch.acc.Virg inia.EDU>, jk********@gmx. net
says...

[ ... ]
> A conforming implementation may have extensions (including additional
library functions), provided they do not alter the behavior of any
well-formed program. Implementations are required to diagnose programs
that use such extensions that are ill-formed according to this
International Standard. Having done so, however, they can compile and
execute such programs.

Note that the standard demands that the diagnostics are given *before*
the program is executed ("Having done so").

By this reading, it's also required to issue the diagnostic *before*
compiling the program. Given that it's normally the compiler the issues
such diagnostics, I think you're reading more into it than is really
there...
I in this context, I would read compiling as "producing the executable". I
agree that the language could be more decisive, however I think, the intent
or at least the desire for compile-time diagnostics is clear.

Also, consider the case where a program is not run (e.g., intermediate
stages in debugging where you just compile to check whether you introduced
typos). Where do you find the language in the standard that waives the
required diagnostics, i.e., where do you find that diagnostics is only
required *if* the program is actually run?

Finally, what about programs like this:

#include <iostream>

int main ( void ) {
int i = 0;
int * p = 0;
if ( std::cin >i ) {
p = 1;
}
}

This program is ill-formed, however, a naive run-time check will only flag
that for certain inputs. I think the standard does not give license to only
flag those ill-formed lines that are executed for a particular input.
Best

Kai-Uwe Bux
Aug 30 '06 #48
Kai-Uwe Bux <jk********@gmx .netwrote:
>Finally, what about programs like this:

#include <iostream>

int main ( void ) {
int i = 0;
int * p = 0;
if ( std::cin >i ) {
p = 1;
}
}
This program is ill-formed, however, a naive run-time check
will only flag that for certain inputs. I think the standard
does not give license to only flag those ill-formed lines that
are executed for a particular input.
A good way to resolve this, but one I don't see in the standard,
is not to allow partial execution of any ill-formed program.
If that is not in the standard, perhaps it is a deliberate omission
to allow interpreters.

Another possibility is that any ability to partially execute an
ill-formed program constitutes a "language extension" and the rules
you quoted previously apply.

Steve
Aug 30 '06 #49
In article <ed**********@m urdoch.acc.Virg inia.EDU>, jk********@gmx. net
says...

[ ... ]
I in this context, I would read compiling as "producing the executable". I
agree that the language could be more decisive, however I think, the intent
or at least the desire for compile-time diagnostics is clear.
In $1.9/1, they say:

This International Standard places no requirement on the
structure of conforming implementations . In particular,
they need not copy or emulate the structure of the abstract
machine. Rather, conforming implementations are required to
emulate (only) the observable behavior of the abstract
machine as explained below.

My guess is that it's a relatively simple situation: most of the authors
are sufficiently accustomed to a compiler type of implementation that
they more or less take it for granted in their writing, even though they
clearly don't intend that it be a requirement.

[ ... ]
This program is ill-formed, however, a naive run-time check will only flag
that for certain inputs. I think the standard does not give license to only
flag those ill-formed lines that are executed for a particular input.
I think from a viewpoint of conformance, this is almost entirely a QOI
issue. The requirement is ($1.4/2):

If a program contains a violation of any diagnosable rule,
a conforming implementation shall issue at least one
diagnostic message, except that If a program contains a
violation of a rule for which no diagnostic is required,
this International Standard places no requirement on
implementations with respect to that program.

Strictly from a viewpoint of conforming, that's trivial to meet: simply
issue a diagnostic before commencing interpretation of anything. If you
wanted to conform without modifying your code, change the documentation
to say your copyright notice was a "diagnostic " and you'd be done.

I'll openly admit that's probably not what anybody wanted or intended,
but I can't think of any part of the standad it'd violate either...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 31 '06 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13064
by: Kevin | last post by:
I was looking through some source code and noticed the used of the C# << operator. Why is this being used here and under what circumstances is an left-shift operator useful. internal enum InterestLevel { Ignore = 0, Display = 1<<0, Interesting = 1<<1, Parents = 1<<2,
2
1643
by: Saber S | last post by:
When I press Alt+Shift in a text control, the cursor turns Right-To-Left. I want it without pressing Alt+Shift, I want to when I click a text control the cursor automatically turn to Right-To-Left.
11
4059
by: Kenneth Lantrip | last post by:
Anyone got any ideas as to how this process could be improved for speed? this is what I have... Dim j, q As Integer Dim x(16), y(16) As Byte x.CopyTo(y, 0) ' shift left circular 24 bits
19
2537
by: aurgathor | last post by:
I use BC 5.02 (long is 32 bit) and I wonder if there's any efficient code that would allow me to left shift 64 bit values that I currently use this way: typedef struct { unsigned long lsLong; unsigned long msLong; } b64_struct;
1
3551
by: jonathanmcdougall | last post by:
I am developing a calendar on which dates can be clicked and selected. A calendar is a table in which each day is a cell. Each cell has a unique id. By left-clicking on a date, it gets selected (with a blue background). By then shift-left-clicking on another date, the range between the first and second dates gets selected. This is a behavior commonly found on GUI widgets. Therefore, I need to cancel the onMouseDown event so the browser...
4
4301
by: sandhya | last post by:
Hello Folks, i hava a problem in coding of circular left shift of 25 bits in my program...how do i perform it, and how do i use unsigned in VB. My program (IDEA algorithm implementation in VB) requires unsigned bits...so how do i go thro this ,since VB does not support unsigned operations Post your suggestions!!!
15
1796
by: Christopher Layne | last post by:
So I recently ran into a situation where I invoked UB without specifically knowing I did it. Yes, human, I know. What exactly is/was the rationale for not allowing shifts to be the same width of the datatype one is shifting? Also, for most common platforms (oh, alright, x86), it's okay to do at the assembly level, isn't it? (provided the opcodes allow it, I guess that's my question as well).
3
2188
by: lokeshrajoria | last post by:
hello everyone, i have problem with left shift...can you solve this.... i want to write a simple function which will give output is 32 bit frame: if input type == 0 - then output will shift left frame id by 18 bits and 31st bit as 0 eg for input configred frame id 0x7ff(111,1111,1111), FrameID= 0x1ffc0000(0001,1111,1111,1100,0000,0000,0000,0000)
4
4221
by: G Iveco | last post by:
I am using this type of code to do right-shifting, B = 3; data1 = (data + (1 << (B-1))) >B; data1 seems incorrect when data = -4-8*i.. which means it rounds -1.5 to -1 instead of -2. On the positive side, 1.5 is rounded to 2, which ic correct.
0
9933
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9781
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11123
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9567
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.