473,387 Members | 1,483 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.

lshift & rshift

Hello guys,
every time a rode a doc or a book about the language C I saw that
operators << and >exist. But each time they said that << translate
the digit to the left (and >...) but no one said if a << 1 mean
every time a * 2, because there is the problem with the way the
integer are stored. So I never use thoses operators because I fear
strange behaviour on different architecture. Can I use them and be
sure that a << 1 means a * 2 on every computer ? Is is in the ansi
standard of language C ?

Sep 27 '07
71 4118
On Thu, 27 Sep 2007 01:26:53 -0700, "ie***@free.fr" <ie***@free.fr>
wrote:
>Hello guys,
every time a rode a doc or a book about the language C I saw that
operators << and >exist. But each time they said that << translate
the digit to the left (and >...) but no one said if a << 1 mean
every time a * 2, because there is the problem with the way the
integer are stored. So I never use thoses operators because I fear
The shift operators work on the value of the integer, not on its
representation. It doesn't matter if the storage scheme is
big-endian, little-endian, or confused-endian. The result is the
same. 1<<n evaluates to the value 2 raised to the power n as long as
the value is in the range of int (the same as repeatedly multiplying
by 2). For a positive value n, n>>1 evaluates to half the value of n
(the same as integer division).
>strange behaviour on different architecture. Can I use them and be
Have no fear. But do take boundary conditions into consideration.
Negative values can behave in an implementation defined manner which
need not be consistent across different systems.
>sure that a << 1 means a * 2 on every computer ? Is is in the ansi
standard of language C ?
There are drafts of the standard readily available on the internet.
Google for n1124.
Remove del for email
Sep 28 '07 #51
<ie***@free.frwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
>Then you shouldn't be fiddling with micro-optimisations.

What ? I used to profile with my hands (like I debug with my hands).
If you just want to _guess_ what's going on, you're never going to get good
results. There's a reason profilers exist -- computers are far, far better
at the task than humans are. Spend your time worrying about writing
_correct_ code and let your tools figure out how to make it fast.
And when a micro operation is used many times it becomes a
macro optimisations. Yeah generally I don't care about a * 2 or
a + a. But sometimes I need to use that 1000..... times so I need
to know which is fastest.
No, you _don't_ need to know which is faster. It makes no sense to spend
effort shaving 1% off a loop that runs a million times (microoptimization)
when you can replace it with a loop that runs only a thousand times
(macrooptimization). For example, a macrooptimization would be looking at a
bubble sort that's running too slowly and replacing it with a quick sort,
i.e. working on the algorithms, not the specific implementation. Compilers
can't do major macrooptimizations (yet), but at least a profiler can tell
you where you need to spend your effort -- and where you don't.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Sep 28 '07 #52
<ie***@free.frwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
So if I want to multiply an int by two I have to use a * 2 (or a + a),
but not << ? To be sure it works on every architecture (currently I
ignore the problem of the size of an int).
If you want correct results, yes. They don't necessarily mean the same
thing. In a case where that microoptimization is safe and correct, any
decent compiler will do it for you, so you're wasting your time.

As the saying goes, "It's easier to make correct code fast than it is to
make fast code correct." Worry about getting the right answer and writing
clean, easy-to-maintain code first. When you've accomplished that, if
there's still a performance problem the compiler can't solve for you, then
you let a profiler tell you what needs fixing.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Sep 28 '07 #53
ie***@free.fr said:
>80% of the cost of software is maintenance.

So write code that is as clear and expressive as possible.

I don't care. When I read my codes I understand them, even those I
wrote 10 years ago, I can't explain why but I never forget one of my
code, I know it's rare.
The reason is simple - you've never written anything complex.

Read and modify others code is a concept which leads to bugs, you
can't say I'm wrong.
From what I've seen of your code, I can see where you might get this idea.
You want to change something. You clear his function and you rewrite
it, but you have to know what are is in and his out, so a great API
reference.
There isn't always enough time for a complete rewrite.
Forget the concept that others will read your code. If they do that
it's because your code is stinky, if it was good they don't need to
have a look.
Or because the requirements have changed. Both kinds are common.
It's like try something and thinking since the beginning
that we are going to fail.
Thinking is not what will make you fail. In your case, your biggest
weaknesses seem to be your preconceptions and your self-image.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 28 '07 #54
<ie***@free.frschrieb im Newsbeitrag
news:11*********************@w3g2000hsg.googlegrou ps.com...
1) What is the optimisation level you have ?

As he said earlier (implicitly): no optimization

No, I used to use -O3, but when I post my example I used no
optimization.
OK, can't guess that from yout posts
2) Does it treat a<<1 and a*2 as a+a for all the optimisation
levels ?

Yes (I've checked)

Ha, interesting.
And I said so in this thread. Exactly identical assembly code for all 3
expressions as of -O1, only a very slight variation in a+a for -O0 resp. no
optimization.
3) What is the architecture you use ?

From the assembly he posted: Intel ix86

686 to be exact (P4 I think).
That's what I used to verify. On a SuSE Linux 10
>>
4) Also tell about the compiler and programming language you use ?

gcc and C

We're in a C forum.
Indeed, but some of your statements in this and other thtreads (sizeof(char)
!= 1) made it difficult to believe that you were talking about C...

Bye, Jojo
Sep 28 '07 #55
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de news:
_b******************************@bt.com...
pete said:
>Richard Heathfield wrote:
>>>
<snip>
>>ch <<= 1; /* ch is now 128 */
ch <<= 1; /* ch is now 0, even though 2 * 128 is 256. */

That example doesn't show a difference
between shifting and a doubling.

I agree. It does, however, show that shifting doesn't always double the
number. The fact that doubling doesn't always double the number is another
issue. :-)
Why unnecessarily confuse the OP ?
left shifting by one is the same as doubling if the value shifted had
unsigned type or signed with a non negative value.

If the type is unsigned, the result of x << 1 is always the same as x + x
and x * 2, even in case of 'overflow' because the standard mandates modular
arithmetics.

If the type is signed, left or right shifting a negative value is
unspecified. The behaviour is implementation defined.

If the value is positive, right shifting by one is the same as dividing by
2, left shifting by one is the same as doubling, and invokes undefined
behaviour in case of overflow, just like addition and multiplication.

--
Chqrlie.
Sep 28 '07 #56
ie***@free.fr wrote:
>80% of the cost of software is maintenance.

So write code that is as clear and expressive as possible.

I don't care. When I read my codes I understand them, even those I
wrote 10 years ago, I can't explain why but I never forget one of my
code, I know it's rare.

Read and modify others code is a concept which leads to bugs, you
can't say I'm wrong.
You want to change something. You clear his function and you rewrite
it, but you have to know what are is in and his out, so a great API
reference.
Welcome to the real world.
Forget the concept that others will read your code. If they do that
it's because your code is stinky, if it was good they don't need to
have a look. It's like try something and thinking since the beginning
that we are going to fail.
No.

I can only assume that you have taken part in even a medium-sized software
project, or a project with other people involved, or a long-term project.

In many situations software needs to be changed because of changing
requirements. That doesn't mean it was written wrongly the first time, it means
that when it was first written, you didn't know what you would need in 5 or 10
years time.

Look at Microsoft Windows. Windows 3.1 didn't provide any support for 3d
graphics cards, because they didn't exist yet. Windows Vista has features that
allow windows to fly all over the place, all made possible by 3d cards.

When I started using the internet, HTML 3.2 was the standard web language. Then
a new version, HTML 4.0, came out. Browsers had to be rewritten to use the new
language, or they would be able to browse the web. CSS was invented. Browsers
had to be rewritten to read stylesheets. The .png file format came out. Browsers
had to be rewritten to display them.

Do you really think Mozilla Firefox, or Microsoft Internet Explorer, could exist
if they had only one developer for their whole lifetime? If not, how could they
be written in such a way that noone ever has to read or modify someone else's
code? What if someone leaves the project?

(When I was 4 years old, I knew everything. As I get older and learn more I seem
to know less and less...)

--
Philip Potter pgp <atdoc.ic.ac.uk
Sep 28 '07 #57
Barry Schwarz wrote:
On Thu, 27 Sep 2007 01:26:53 -0700, "ie***@free.fr" <ie***@free.fr>
wrote:
>sure that a << 1 means a * 2 on every computer ? Is is in the ansi
standard of language C ?

There are drafts of the standard readily available on the internet.
Google for n1124.
n1256 has superceded n1124. But I wouldn't recommend you read it cover-to-cover.

--
Philip Potter pgp <atdoc.ic.ac.uk
Sep 28 '07 #58
<ro***********@yahoo.coma écrit dans le message de news:
11**********************@19g2000hsx.googlegroups.c om...
On Sep 27, 8:07 am, "ie...@free.fr" <ie...@free.frwrote:
>In beginning my question was theoretical.
The real meaning was, is << a left shift or something who shift the
numbers from the less important bit to the most important.
And people said it's the second, not the first.


The C standard defines a<<b to be a shift operations, and further
defines that it will be equivalent to ((a*(2**b))mod Uxxx_MAX) for
unsigned a's and non-negative b's.

For signed first operands, if the first operand is non-negative, and
if the result of (a*(2**b)) is representable in the type, then that's
the result. Otherwise (in the case of overflow or negative first or
second operands) the result is undefined.

In both cases, shift counts larger than the first parameter's width in
bits also have undefined results.
larger or equal.

6.5.7p3:
The integer promotions are performed on each of the operands. The type of
the result is
that of the promoted left operand. If the value of the right operand is
negative or is
greater than or equal to the width of the promoted left operand, the
behavior is undefined.

On a 'regular' 32-bit machine, we have this:

0 << 31 defined, result is 0
1u << 31 defined, result is 0x80000000u
1 << 31 undefined behaviour
1 << -1 undefined behaviour
0 << 32 undefined behaviour

--
Chqrlie.
Sep 28 '07 #59
Do you really think Mozilla Firefox, or Microsoft Internet Explorer, could exist
if they had only one developer for their whole lifetime? If not, how could they
be written in such a way that noone ever has to read or modify someone else's
code? What if someone leaves the project?
With the modularity, others don't need to read your code, they just
have to know in and out. Modularity it's not you take a piece and you
transform it, it's you take a piece and you replace it. You care about
what the old did not how it did (it's not your business). You can also
add a new one.

Sep 28 '07 #60
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@r29g2000hsg.googlegr oups.com...
>Do you really think Mozilla Firefox, or Microsoft Internet Explorer,
could exist
if they had only one developer for their whole lifetime? If not, how
could they
be written in such a way that noone ever has to read or modify someone
else's
code? What if someone leaves the project?

With the modularity, others don't need to read your code, they just
have to know in and out. Modularity it's not you take a piece and you
transform it, it's you take a piece and you replace it. You care about
what the old did not how it did (it's not your business). You can also
add a new one.
So you'd rather reinvent the wheel than to make an existing one rounder? Way
too expensive in the real world. Better hire someone who can make pretty
round wheels in the first place, ones that are maintainable and enables
others to base their work on.

Bye, Jojo
Sep 28 '07 #61
So you'd rather reinvent the wheel than to make an existing one rounder? Way
too expensive in the real world. Better hire someone who can make pretty
round wheels in the first place, ones that are maintainable and enables
others to base their work on.
You're misquoting me.

Sep 28 '07 #62

<ie***@free.frschrieb im Newsbeitrag
news:11**********************@22g2000hsm.googlegro ups.com...
>So you'd rather reinvent the wheel than to make an existing one rounder?
Way
too expensive in the real world. Better hire someone who can make pretty
round wheels in the first place, ones that are maintainable and enables
others to base their work on.

You're misquoting me.
I didn't change a single letter
Sep 28 '07 #63
ie***@free.fr wrote:
>Do you really think Mozilla Firefox, or Microsoft Internet Explorer,
could exist if they had only one developer for their whole lifetime? If
not, how could they be written in such a way that noone ever has to read
or modify someone else's code? What if someone leaves the project?

With the modularity, others don't need to read your code, they just
have to know in and out. Modularity it's not you take a piece and you
transform it, it's you take a piece and you replace it. You care about
what the old did not how it did (it's not your business). You can also
add a new one.
In a team project, at some level, you've got to know how to read and modify
other people's code. Even if other modules are treated as completely opaque
black boxes, you still need to know about your module, which is itself, in
most projects written by multiple people.

Sep 28 '07 #64
On Sep 28, 4:27 pm, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
<ie...@free.frschrieb im Newsbeitragnews:11**********************@22g2000hs m.googlegroups.com...>So you'd rather reinvent the wheel than to make an existing one rounder?
Way
too expensive in the real world. Better hire someone who can make pretty
round wheels in the first place, ones that are maintainable and enables
others to base their work on.
You're misquoting me.

I didn't change a single letter
I used the bad verb.
I never mean reinvent the wheel.
And normally (like all my friends who work on project with many people
said me) a file must be one page long, and the function must not
surpass 10 or 20 lines.

So rewrite a module is not a long work. But a code has to be well cut,
if it's not it's because it's stinky. And it's there where we see the
skill of a developer or of the project manager.

Sep 28 '07 #65
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@22g2000hsm.googlegro ups.com...
>>So you'd rather reinvent the wheel than to make an existing one rounder?
Way
too expensive in the real world. Better hire someone who can make pretty
round wheels in the first place, ones that are maintainable and enables
others to base their work on.

You're misquoting me.
I didn't change a single letter
No, but you didn't seem to fully understand what he said.

What he said was actually correct with regard to modularity.

You don't tinker with good modules to make them "more round" - god knows
what else you might break which relies on that module being not quite so
round.

Sep 28 '07 #66
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@r29g2000hsg.googlegr oups.com...
On Sep 28, 4:27 pm, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
><ie...@free.frschrieb im
Newsbeitragnews:11**********************@22g2000h sm.googlegroups.com...>>
So you'd rather reinvent the wheel than to make an existing one rounder?
>Way
too expensive in the real world. Better hire someone who can make
pretty
round wheels in the first place, ones that are maintainable and
enables
others to base their work on.
You're misquoting me.

I didn't change a single letter

I used the bad verb.
I never mean reinvent the wheel.
If you throw away a module and create a new one, you do just that
And normally (like all my friends who work on project with many people
said me) a file must be one page long, and the function must not
surpass 10 or 20 lines.
uhh, in that case... while I agree that a code file with several 1000 lines
is bad and hard to maintain, just one page (assuming 72 lines here?) is
pretty restrictive, as is 20 lines per function.
So rewrite a module is not a long work. But a code has to be well cut,
It adds up. With sich short functions you'd have many of them and changing
some funtionality would most probably involve changing many functions...
if it's not it's because it's stinky. And it's there where we see the
skill of a developer or of the project manager.

Sep 28 '07 #67
"ro***********@yahoo.com" <ro***********@yahoo.comwrites:
On Sep 27, 8:07 am, "ie...@free.fr" <ie...@free.frwrote:
>In beginning my question was theoretical.
The real meaning was, is << a left shift or something who shift the
numbers from the less important bit to the most important.
And people said it's the second, not the first.


The C standard defines a<<b to be a shift operations, and further
defines that it will be equivalent to ((a*(2**b))mod Uxxx_MAX) for
unsigned a's and non-negative b's.
.... provided b is less than the width of the type of the result
(undefined, otherwise).

--
Ben.
Sep 28 '07 #68
<ie***@free.fra écrit dans le message de news:
11**********************@r29g2000hsg.googlegroups. com...
On Sep 28, 4:27 pm, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
><ie...@free.frschrieb im
Newsbeitragnews:11**********************@22g2000h sm.googlegroups.com...>>
So you'd rather reinvent the wheel than to make an existing one rounder?
>Way
too expensive in the real world. Better hire someone who can make
pretty
round wheels in the first place, ones that are maintainable and
enables
others to base their work on.
You're misquoting me.

I didn't change a single letter

I used the bad verb.
I never mean reinvent the wheel.
And normally (like all my friends who work on project with many people
said me) a file must be one page long, and the function must not
surpass 10 or 20 lines.

So rewrite a module is not a long work. But a code has to be well cut,
if it's not it's because it's stinky. And it's there where we see the
skill of a developer or of the project manager.
project with many people = classroom
one page file = simple programming assignment (one function)
project manager = teacher

What grade are you in ?
Sep 29 '07 #69
Charlie Gordon wrote:
>
<ie***@free.fra écrit dans le message de news:
11*********************@r29g2000hsg.googlegroups.c om...
Forget the concept that others will read your code. If they do that
it's because your code is stinky, if it was good they don't need to
have a look.
You must be a troll
I think it's just that he's fantasizing
about what it would be like to be a programmer;
and also that he has never met a programmer
and has absolutlely no idea of what he's talking about.

--
pete
Sep 29 '07 #70
"pete" <pf*****@mindspring.comwrote in message
news:46**********@mindspring.com...
Charlie Gordon wrote:
><ie***@free.fra écrit dans le message de news:
11*********************@r29g2000hsg.googlegroups.c om...
Forget the concept that others will read your code. If they do that
it's because your code is stinky, if it was good they don't need to
have a look.
>You must be a troll

I think it's just that he's fantasizing about what it would be like to
be a programmer; and also that he has never met a programmer
and has absolutlely no idea of what he's talking about.
I've known many professional programmers that think like this guy does. No
_good_ ones, though.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Sep 29 '07 #71
On Sep 27, 8:26 pm, "ie...@free.fr" <ie...@free.frwrote:
Hello guys,
every time a rode a doc or a book about the language C I saw that
operators << and >exist. But each time they said that << translate
the digit to the left (and >...) but no one said if a << 1 mean
every time a * 2, because there is the problem with the way the
integer are stored.
For unsigned integers, or for non-negative values of
signed integers, << 1 means exactly the same as * 2 .

For negative integers, left-shifting causes undefined
behaviour.

Oct 1 '07 #72

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

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
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
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...
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.