473,883 Members | 2,621 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 15370

Victor Bazarov wrote:
Whatever conclusions you're trying to make from whatever knowledge you
possess about the hardware are nonconsequentia l; the Standard says that
the behaviour of the program in which the right operand of the shift
operator is negative or greater than or equal to the length in bits of
the promoted left operand is *undefined*. It can be 0, it can be 1, or
(and you know the drill, repeat after me) it can make demons fly out of
your nose.
Is the behaviour also undefined if you shifted by 1 bit 32 times? Are
the two equivalent?

In that case ((1 << 16) << 16) would be fully defined as long as
sizeof(int) 16.

I assume, by the way, that it is perfectly defined behaviour to
right-shift bits off the end because that I do regularly, eg
right-shift 0x1234 >8 should give me 0x12. (If you're not allowed to
do that there is something horribly wrong with the standard).

Aug 25 '06 #11
Earl Purple wrote:
Victor Bazarov wrote:
>Whatever conclusions you're trying to make from whatever knowledge
you possess about the hardware are nonconsequentia l; the Standard
says that the behaviour of the program in which the right operand of
the shift operator is negative or greater than or equal to the
length in bits of the promoted left operand is *undefined*. It can
be 0, it can be 1, or (and you know the drill, repeat after me) it
can make demons fly out of your nose.

Is the behaviour also undefined if you shifted by 1 bit 32 times? Are
the two equivalent?
No, they are not equivalent. Shifting by 1 bit is well-defined. No
matter how many times you do it.
In that case ((1 << 16) << 16) would be fully defined as long as
sizeof(int) 16.
Correct.
I assume, by the way, that it is perfectly defined behaviour to
right-shift bits off the end because that I do regularly, eg
right-shift 0x1234 >8 should give me 0x12. (If you're not allowed to
do that there is something horribly wrong with the standard).
It's well-defined. Get a copy of the Standard to check.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #12

"Earl Purple" <ea********@gma il.comskrev i meddelandet
news:11******** **************@ i3g2000cwc.goog legroups.com...
>
Victor Bazarov wrote:
>Whatever conclusions you're trying to make from whatever knowledge
you
possess about the hardware are nonconsequentia l; the Standard says
that
the behaviour of the program in which the right operand of the
shift
operator is negative or greater than or equal to the length in bits
of
the promoted left operand is *undefined*. It can be 0, it can be
1, or
(and you know the drill, repeat after me) it can make demons fly
out of
your nose.

Is the behaviour also undefined if you shifted by 1 bit 32 times?
Are
the two equivalent?

In that case ((1 << 16) << 16) would be fully defined as long as
sizeof(int) 16.
The restriction in the standard is there because of the underlying
hardware.

We expect the compiler to be able to use the shift instructions of the
CPU. The problem is that some hardware will actually perform the long
shift (with a zero result), while others will do it modulo register
size. Others might refuse altogether.

With the popular x86 family, the behaviour has changed between
generations, which makes it hard for the compiler to compensate.

So, the C++ standard says that a shift works as expected as long as
the shift count is smaller than the integer type. If it is larger we
don't know, that's up to the underlying hardware.
Bo Persson
Aug 25 '06 #13
Marcus Kwok wrote:
>
At this point, since an int is 8 bits, we can see why we can only shift
a maximum of 7. When it gets to (1 << 8), what should it do? Shift the
1 completely out of the number? Or leave it where it is? I'm not sure
but this is probably one of {undefined, unspecified,
implementation-defined}.
Undefined behavior. On the Intel i860, there is only enough bits
in the shift instruction to encode 31 places. If you do 32, it
only stuffs the low order bits and you get NOTHING. Believe me
the @&$@ X11 Server code relied on that yielding a zero not the
original operand. Had to fix it in a dozen places.

Aug 26 '06 #14
In article <44************ ***********@new s.newshosting.c om>,
ro*@spamcop.net says...

[ ... ]
Undefined behavior. On the Intel i860, there is only enough bits
in the shift instruction to encode 31 places. If you do 32, it
only stuffs the low order bits and you get NOTHING. Believe me
the @&$@ X11 Server code relied on that yielding a zero not the
original operand. Had to fix it in a dozen places.
The x86 allows you to encode more if you want to, but recent iterations
of the processor mask it to 5 bits before doing the shift.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 26 '06 #15
Jerry Coffin <jc*****@taeus. comwrote:
>ro*@spamcop.ne t says...
>Undefined behavior. On the Intel i860, there is only enough bits
in the shift instruction to encode 31 places. If you do 32, it
only stuffs the low order bits and you get NOTHING. Believe me
the @&$@ X11 Server code relied on that yielding a zero not the
original operand. Had to fix it in a dozen places.
>The x86 allows you to encode more if you want to, but recent iterations
of the processor mask it to 5 bits before doing the shift.
And I still say for in the code fragment posted, no left shift
instruction will ever be emitted by the compiler, so such details are
quite possibly irrelevant.

Steve
Aug 26 '06 #16
In article <ec**********@b lue.rahul.net>, sp*****@speedym ail.org says...

[ ... ]
And I still say for in the code fragment posted, no left shift
instruction will ever be emitted by the compiler, so such details are
quite possibly irrelevant.
This seems almost self-contradictory. If you could be certain the
compiler wouldn't emit a left-shift instruction, then such details would
be definitely irrelevant. The only way they may be relevant is if the
compiler might emit the instruction -- which I'd say is correct for at
least a few compilers.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 26 '06 #17
Jerry Coffin <jc*****@taeus. comwrote:
sp*****@speedym ail.org says...
>And I still say for in the code fragment posted, no left shift
instruction will ever be emitted by the compiler, so such details are
quite possibly irrelevant.
This seems almost self-contradictory. If you could be certain
the compiler wouldn't emit a left-shift instruction, then such
details would be definitely irrelevant. The only way they may
be relevant is if the compiler might emit the instruction --
which I'd say is correct for at least a few compilers.
That is why I said "possibly irrelevant". "Intuitivel y, constant
expressions are simple expressions that can be evaluated by the
compiler before the program is linked [9.1] and starts to run."
(Stroustrap, C.5) To me it's unintuitive (but not incorrect) that
the compiler would not reduce the expression.

And actually, if the constant expression reduces to zero and is
assigned to a pointer, it's probably incorrect for the compiler
not to reduce it; but maybe those more expert than I might have
an opinion on this.

Steve
Aug 26 '06 #18
Following up to myself:

Steve Pope <sp*****@speedy mail.orgwrote:
>Jerry Coffin <jc*****@taeus. comwrote:
>sp*****@speedym ail.org says...
>>And I still say for in the code fragment posted, no left shift
instruction will ever be emitted by the compiler [..]
>This seems almost self-contradictory. If you could be certain
the compiler wouldn't emit a left-shift instruction, then such
details would be definitely irrelevant. The only way they may
be relevant is if the compiler might emit the instruction --
which I'd say is correct for at least a few compilers.
>That is why I said "possibly irrelevant". "Intuitivel y, constant
expressions are simple expressions that can be evaluated by the
compiler before the program is linked [9.1] and starts to run."
(Stroustrap, C.5) To me it's unintuitive (but not incorrect) that
the compiler would not reduce the expression.
>And actually, if the constant expression reduces to zero and is
assigned to a pointer, it's probably incorrect for the compiler
not to reduce it; but maybe those more expert than I might have
an opinion on this.
In fact, the following compiles (gcc):

int foo() {
int *pi;
pi = 1 << 100;
}

It seems to me that any approach that did not perform the
left shift until runtime would not be a correct implementation.

(Although there are some screwy possibilties, such as only
reducing the constant expression at compile time when it's
being assigned to a pointer; or reducing if it reduces to zero,
and issuing code for it otherwise; but I think these possibilities
are very highly remote.)

Steve
Aug 27 '06 #19

"Steve Pope" <sp*****@speedy mail.orgskrev i meddelandet
news:ec******** **@blue.rahul.n et...
Jerry Coffin <jc*****@taeus. comwrote:
>sp*****@speedym ail.org says...
>>And I still say for in the code fragment posted, no left shift
instruction will ever be emitted by the compiler, so such details
are
quite possibly irrelevant.
>This seems almost self-contradictory. If you could be certain
the compiler wouldn't emit a left-shift instruction, then such
details would be definitely irrelevant. The only way they may
be relevant is if the compiler might emit the instruction --
which I'd say is correct for at least a few compilers.

That is why I said "possibly irrelevant". "Intuitivel y, constant
expressions are simple expressions that can be evaluated by the
compiler before the program is linked [9.1] and starts to run."
(Stroustrap, C.5) To me it's unintuitive (but not incorrect) that
the compiler would not reduce the expression.

And actually, if the constant expression reduces to zero and is
assigned to a pointer, it's probably incorrect for the compiler
not to reduce it; but maybe those more expert than I might have
an opinion on this.
Do you mean that compile time constant expressions should be evaluated
differently than non-constant expressions? Shouldn't we expect the
same result?
To be more technical, the standard doesn't talk about compilers at
all - we could very well have an interpreter, or some other kind of
translator. And *if* a compiler does a compile time evaluation, is it
not supposed to use the hardware instructions itself, internally?
Bo Persson


Aug 27 '06 #20

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
11125
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...
1
10836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9568
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...
1
7962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4607
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
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.