473,666 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with VC++ signed and unsigned __int64 << and >> operators?

The following code snippet does not seem to work correcly

unsigned __int64 result = 0xFFFFFFFFFFFFF FFF;
result = result << 64;
Debugger.WriteL ine(System::Str ing::Format(S"{ 0:X16}", __box(result));
result = 0xFFFFFFFFFFFFF FFF;
result = result >> 64;
Debugger.WriteL ine(System::Str ing::Format(S"{ 0:X16}", __box(result));

__int64 result = -1;
result = result << 64;
Debugger.WriteL ine(System::Str ing::Format(S"{ 0:X16}", __box(result));

I am expecting output to read

000000000000000 0
FFFFFFFFFFFFFFF F
000000000000000 0
FFFFFFFFFFFFFFF F

but output reads

FFFFFFFF0000000 0 << incorrect
00000000FFFFFFF F << incorrect
FFFFFFFF0000000 0 << incorrect
FFFFFFFFFFFFFFF F << correct

All other shift values ( < 64) appear to work correctly.

Can someone from MS confirm this is a bug with the int64 shift
operators, please?

Peter

--
If you wish to reply to me directly, my addres is spam proofed as:

pbromley at adi dot co dot nz

Or if you prefer - no****@nowhere. com :-)

Nov 17 '05 #1
17 3029
Peter Bromley wrote:
The following code snippet does not seem to work correcly

unsigned __int64 result = 0xFFFFFFFFFFFFF FFF;
result = result << 64;
Debugger.Write Line(System::St ring::Format(S" {0:X16}", __box(result));
result = 0xFFFFFFFFFFFFF FFF;
result = result >> 64;
Debugger.Write Line(System::St ring::Format(S" {0:X16}", __box(result));

__int64 result = -1;
result = result << 64;
Debugger.Write Line(System::St ring::Format(S" {0:X16}", __box(result));

I am expecting output to read

00000000000000 00
FFFFFFFFFFFFFF FF
00000000000000 00
FFFFFFFFFFFFFF FF

but output reads

FFFFFFFF000000 00 << incorrect
00000000FFFFFF FF << incorrect
FFFFFFFF000000 00 << incorrect
FFFFFFFFFFFFFF FF << correct

All other shift values ( < 64) appear to work correctly.


It's undefined to shift a value by an amount >= its length in bits, so you
need to avoid doing that.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
> It's undefined to shift a value by an amount >= its length in bits, so you
need to avoid doing that.


This thru for mc++ only, right? Shifting in x86 is well defined, for
unsigned 0s are shifted in, for signed the sign bit is extended when needed.
I wonder what can be the reason why the same code compiled for .net
shouldn't give the same results as its native counterpart.
Nov 17 '05 #3

"Gabest" <ga****@freemai l.hu> wrote in message
news:e6******** ******@TK2MSFTN GP14.phx.gbl...
It's undefined to shift a value by an amount >= its length in bits, so
you
need to avoid doing that.


This thru


"is true" I mean. It's really late here :)
Nov 17 '05 #4
Gabest wrote:
It's undefined to shift a value by an amount >= its length in bits,
so you need to avoid doing that.


This thru for mc++ only, right? Shifting in x86 is well defined, for
unsigned 0s are shifted in, for signed the sign bit is extended when
needed. I wonder what can be the reason why the same code compiled
for .net shouldn't give the same results as its native counterpart.


No, it's true for C and C++ in general - shifting by an amount >= the
variable size is undefined behavior according to the standards.

-cd
Nov 17 '05 #5
Well, after digging up intel's manual on these shifting instructions, it is
defined but not in the way I though to be:

"The count operand can be an immediate value or register CL. The count is
masked to 5 bits, which limits the count range to 0 to 31"

I believe amd can have something similar for the x86-64 ISA in its manuals
too, and also that in 32 bit mode the compiler inserts such a piece of code
what still gives a good result for its builtin __int64 type, but only for
the native code.
Nov 17 '05 #6
Gabest wrote:
It's undefined to shift a value by an amount >= its length in bits, so you
need to avoid doing that.


This thru for mc++ only, right? Shifting in x86 is well defined, for
unsigned 0s are shifted in, for signed the sign bit is extended when needed.
I wonder what can be the reason why the same code compiled for .net
shouldn't give the same results as its native counterpart.


What I said comes from the C++ Standard, 5.8/1. (To be completely accurate,
s/value/promoted left operand/, but note that a 64-bit integer undergoes no
promotion in VC++.) The VC docs say the same thing:

http://msdn.microsoft.com/library/de..._operators.asp
<q>
The results are undefined if the right operand of a shift expression is
negative or if the right operand is greater than or equal to the number of
bits in the (promoted) left operand.
</q>

I'm unable to find any information on the shift operators in MC++, but even
if the result were well-defined, I'd hesitate to take advantage of it.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #7
Gabest wrote:
This thru for mc++ only, right? Shifting in x86 is well defined, for
unsigned 0s are shifted in, for signed the sign bit is extended when needed.
I wonder what can be the reason why the same code compiled for .net
shouldn't give the same results as its native counterpart.

I suppose this happens because in managed applications you do not
program against your x86 but the .NET VM which has its own assembly
language etc.

So your x86 system-specific code does not work the same to this
different "machine".

To see detailed technical info for .NET (CLI) specific code, you may
download the latest CLI standard:

http://www.ecma-international.org/pu...s/Ecma-335.htm

which also contains the specification of its assembly language.
A book on this assembly language:

http://www.amazon.com/exec/obidos/tg...glance&s=books

--
Ioannis Vranos
Nov 17 '05 #8
Gabest <ga****@freemai l.hu> wrote:
Well, after digging up intel's manual on these shifting instructions, it is
defined but not in the way I though to be:

"The count operand can be an immediate value or register CL. The count is
masked to 5 bits, which limits the count range to 0 to 31"

I believe amd can have something similar for the x86-64 ISA in its manuals
too, and also that in 32 bit mode the compiler inserts such a piece of code
what still gives a good result for its builtin __int64 type, but only for
the native code.

The problem is that you're not programming
in assembler/machine code. If you're in
C/C++/MC++ land, you have to follow the
rules as spelled out there. For C++ (and
most likely for C, too), what you do is
undefined. As for MC++ -- I don't know
that.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
Nov 17 '05 #9
> The problem is that you're not programming
in assembler/machine code.
Yea, you are right, I was believing too much that the compiler would just
use the existing assembly instructions for shifting, while it has the right
to translate it to anything else of course.

There is still one thing but bothers me though. This means you cannot be
sure about shifting by a+b always gives the same result as shifting by a and
then b separately. Something like saying 1+2+3 equals to 6, but the result
of 1+5 is undefined. I can understand shl/shr/sar can have some limitations,
since they are just low level instructions translated directly to machine
code, but in c/c++ this sounds pretty wierd to me.
If you're in
C/C++/MC++ land, you have to follow the
rules as spelled out there. For C++ (and
most likely for C, too), what you do is
undefined. As for MC++ -- I don't know
that.

Schobi

Nov 17 '05 #10

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

Similar topics

3
3214
by: robert | last post by:
Hi All, I have a problem. I have written a COM DLL in VB6 to do some stuff. I need to call this DLL from a VC++ program for which I have the code. I have managed to get as far as creating the instance of the COM object and calling a method from within the C++ program - not being a C++ programmer at all, this was a huge undertaking, and I'm very proud of myself for even getting this far ;-). My Question is in relation to passing...
9
4951
by: Fred Ma | last post by:
Hello, I've been trying to clear up a confusion about integer promotions during expression evaluation. I've checked the C FAQ and C++ FAQ (they are different languages, but I was hoping one would clear up the confusion), as well as googling groups and the web. The confusion is that for a binary operator,
2
2130
by: Rahul | last post by:
Hi, I have a little program as follows : =================== STARTS HERE ================ #include <stdio.h> void f (unsigned long); int main() {
1
1656
by: JezB | last post by:
I'm binding a DataGrid web-control to data fetched from a database. However some of my data fields contain text that is within <...> characters - I notice that everything between the <> is suppressed in the grid. How can I resolve this ?
3
1501
by: ruskie | last post by:
I created a user control with two public properties and drop it to an aspx page as the following <uc1:myUserControl id="myUserControl1" runat="server" MyProperty1="<%= this.MyVar1 %>" MyProperty2="<%= this.MyVar2 %>" > </uc1:myUserControl>
9
3673
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it into a struct timeval object. I get the ByteBuffer as an array of const unsigned char, 'buffer'. Here's an example: 00 00 01 0A 29 1D 07 E4 This value maps somehow to Thu Mar 23 16:57:49 2006 and some
3
2356
by: Nate Barney | last post by:
I have: // base class for Vector and Matrix template <unsigned N,typename Value=float> class NonScalar { public: Value *ptr() { return e; } const Value *ptr() const { return e; }
3
3368
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above functionality without having to use &gt,&lt and such stuff???
7
2619
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is returned. I tend to believe that MSVC made a big mistake here!!! But unfortunately I must use it at work, which sucks!!!
0
8448
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
8356
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
8871
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
8783
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6198
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
5666
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.