473,563 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What will happen to "1 << (-1)" in G++?

Left shift by negative numbers, will I get 1/2?

Thanks.

Sep 8 '06 #1
16 2010
Left shift by negative numbers, will I get 1/2?

Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.

Sep 8 '06 #2
"Mr. Ken" <Mr. Ken@asdfschrieb im Newsbeitrag
news:45******** @news.starhub.n et.sg...
Left shift by negative numbers, will I get 1/2?
The result of a shift expression is undefined if its right operand is
negative or greater than or equal to the number of bits used to represent
its left operand.

Heinz

Sep 8 '06 #3

"Heinz Ozwirk" <ho*****@arcor. dewrote in message
news:45******** **************@ newsspool1.arco r-online.net...
"Mr. Ken" <Mr. Ken@asdfschrieb im Newsbeitrag
news:45******** @news.starhub.n et.sg...
Left shift by negative numbers, will I get 1/2?

The result of a shift expression is undefined if its right operand is
negative or greater than or equal to the number of bits used to represent
its left operand.

Heinz
Thank you.

Sep 8 '06 #4

"Robert J. Hansen" <ci********@gma il.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Left shift by negative numbers, will I get 1/2?

Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.
I received code from other people, but it has the probability of 1 << (-1)..
that's why I asked.

Sep 8 '06 #5
Robert J. Hansen wrote:
Left shift by negative numbers, will I get 1/2?

Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.
this is Really Bad Advice. I you want t to know what a language
construct
is supposed to do then check out a reference. The language standard
itself
is often the best. If you distrust your implementation you may want to
run
a test to verify that your implementation is conforming.

Since "1 << (-1)" has undefined behaviour no amount of empirical
testing
will tell you anything. This weeks test may be invalidated next week by
a
change of platform, compiler version or optimisation settings. In
principal
an implementation could be even worse than that. It may depend on some
obscurity in the hardware.
--
Nick Keighley

Egon: Try to imagine all life as you know it stopping instantaneously
and
every molecule in your body exploding at the speed of light.
Ray: Total protonic reversal....
[DS9000K: Behaviour of Negative Shift]

Sep 8 '06 #6
Mr. Ken wrote:
Left shift by negative numbers, will I get 1/2?
Lame troll.

Sep 8 '06 #7
Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.

this is Really Bad Advice.
It is really _good_ advice, given that he specifically asked a question
tied to a specific implementation. If he wants to know what happens in
G++ when this is done, the best way is to try it for yourself. It's
not as if the compiler costs a lot of money to download.

Sep 8 '06 #8
Sometimes a bad advice.

Yes. If he were to be asking "according to the Standard, what happens
when...?", then I would have given him an answer citing the Standard.

But if you're going to ask compiler-specific questions, then "try it
yourself and see" is perfectly reasonable advice. Especially when the
real answer is "it's undefined and the compiler can do anything, and so
the only answer to this question of 'what will G++ do with this?' is
'give it to G++ and see'."

Sep 8 '06 #9
Robert J. Hansen wrote:
>Sometimes a bad advice.

Yes. If he were to be asking "according to the Standard, what happens
when...?", then I would have given him an answer citing the Standard.

But if you're going to ask compiler-specific questions, then "try it
yourself and see" is perfectly reasonable advice. Especially when the
real answer is "it's undefined and the compiler can do anything, and so
the only answer to this question of 'what will G++ do with this?' is
'give it to G++ and see'."
Except that G++ could do it differently each and every time you run the
program.

--
Clark S. Cox III
cl*******@gmail .com
Sep 8 '06 #10

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

Similar topics

18
2919
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
2
1196
by: Xiangliang Meng | last post by:
Hi, all. What will happen if a virtual function is declared to be virtual again in a derived class? Any harm? Best Regards, Xiangliang Meng
4
1351
by: Anil | last post by:
Question related to ASP.Net web application with .Net Framework 1.1. I am using ADO.Net and ODBC namespace. When the user clicks the process button I am doing database INSERT/UPDATRE.. activity and it will take 30 seconds to complete the database activity and then the user will be redirected to different screen.
67
3739
by: neilcancer | last post by:
i come from china,and i'm sorry that my english is very poor. now i'm studing data structure and i met some problem about c language. could you tell me what will happen after i use free()? i mean once i use free() on a pointer,what will the pointer points to ? for example: #include<stdio.h>
17
6784
by: Ravi | last post by:
void main() { main(); } int main() { main(); }
10
1702
by: Shraddha | last post by:
int *s=(int *)2000; int *p=(int *)1000; printf("%d",p-s);
3
1703
by: vimalankvk80 | last post by:
what will happen, if we forget to return ostream referance ? class A { Private: int _a; int _b;
11
1491
by: active | last post by:
If I install .NET Framework 3.0 what will happen to my VS2005 experience? Will it automatically use 3.0? Will I find new features available? Will the VS doc be updated? Thanks fir any info
0
7658
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...
0
7877
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. ...
0
8101
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...
1
7631
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...
0
6238
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...
1
5479
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...
0
3631
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...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.