lshift & rshift

C

Charlie Gordon

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
 
I

iesvs

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.
 
J

Joachim Schmitz

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
 
I

iesvs

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.
 
S

santosh

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.
 
R

Richard

Joachim Schmitz said:
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.
 
I

iesvs

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.
 
J

Joachim Schmitz

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...
 
B

Ben Bacarisse

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).
 
C

Charlie Gordon

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.

It depends, are you 13 ?
Read and modify others code is a concept which leads to bugs, you
can't say I'm wrong.

Code reviewing *is* the process of reading other peoples code in order to
find and correct bugs.
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.

That's a good way to produce new bugs.
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.

You must be a troll
 
C

Charlie Gordon

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 ?
 
S

Stephen Sprunk

pete said:
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
 
O

Old Wolf

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top