473,387 Members | 1,561 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,387 software developers and data experts.

Strangeness with shifts

The behaviour of << and >> arithmetic operators (under VC++6, x86) seems
rather odd to me.

Shifts at runtime seem to only use the bottom 5 bits of the shift amount (an
x86 issue I suppose), but the compile time evaluation will do as you'd
expect (so i << 32 is 0, rather than i).

Also compile time shifts do some weird clamping where, e.g.
-99 >> 20 is -1
and -99 >> -1 is -1

Does any of this seem acceptable behaviour (particularly the fact that a
simple arithmetic operation can have different results and compile and
runtime) ?
thanks.


Jul 22 '05 #1
2 1681
"Glen Able" <re***********************@hoxtmxail.com> writes:
Does any of this seem acceptable behaviour (particularly the fact that a
simple arithmetic operation can have different results and compile and
runtime) ?


According to the standard, behavior in these cases is undefinded, so
the implementation is correct.
Jul 22 '05 #2
On Sun, 23 Nov 2003 11:38:40 -0000, "Glen Able"
<re***********************@hoxtmxail.com> wrote in comp.lang.c++:
The behaviour of << and >> arithmetic operators (under VC++6, x86) seems
rather odd to me.

Shifts at runtime seem to only use the bottom 5 bits of the shift amount (an
x86 issue I suppose), but the compile time evaluation will do as you'd
expect (so i << 32 is 0, rather than i).

Also compile time shifts do some weird clamping where, e.g.
-99 >> 20 is -1
and -99 >> -1 is -1

Does any of this seem acceptable behaviour (particularly the fact that a
simple arithmetic operation can have different results and compile and
runtime) ?
thanks.


From the ISO C++ standard:

=====
[expr.shift] 5.8 Shift operators
1 The shift operators << and >> group left to right.

[snip grammar]

The operands shall be of integral or enumeration type and integral
promotions are performed. The type of the result is that of the
promoted left operand. The behavior is undefined if the right operand
is negative, or greater than or equal to the length in bits of the
promoted left operand.
=====

So when you invoke undefined behavior with a right operand of greater
than 31 (for Visual C++), the C++ language no longer knows nor cares
what happens. When you generate undefined behavior, you most
certainly have different results even with "simple arithmetic
operation".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #3

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

Similar topics

2
by: Robert M. Gary | last post by:
I'm using JRE 1.5 on Solaris Japanese (Sparc). The JVM claims its default character set is EUC-JP I'm seeing two strange things when using Japanese character sets... 1) If I write a program that...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
4
by: Dave | last post by:
Hello - Say I have a 32 bit value in data (v:4, r:4, l:8, p:16) unsigned char data; Now, for portabilities sake, I want to use shifts and masks to access these fields, instead of accessing...
3
by: Dave | last post by:
I need to rewrite the following using shifts and masks for portability sakes, and to get rid of the BYTE_ORDER issue. /* CODE */ struct test { #if BYTE_ORDER == BIG_ENDIAN unsigned char ...
3
by: larzeb2000 | last post by:
I have constructed a css stylesheet which has a centered layout. The wrapper div is defined as 1000px. It contains a header div for the entire wrapper width, and a left sidebar div and a content...
3
by: geraldshastri | last post by:
Hi, I am designing xhtml templates using table-less design with div's and CSS positioning. But the problem is that certain pages tend to shift to the left despite the fact that the underlying...
15
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...
2
by: skijor | last post by:
For a page that display's a catalogue of items in table format. If the number of rows extends below the view, in some browsers (safari, firefox) the page shifts to the left a little bit. It's...
10
by: dave | last post by:
Hello, I made a function that takes a word list (one word per line, text file) and searches for all the words in the list that are 'shifts' of eachother. 'abc' shifted 1 is 'bcd' Please take...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.