473,396 Members | 2,147 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,396 software developers and data experts.

Why does the value get discarded in this case?

When I have:

int main(void)
{
int x = 256;
x>>8;

printf("The value is: %d\n", x);
return 0;
}

I get:

[cdalten@localhost ~]$ gcc -g -Wall seq.c -o seq
seq.c: In function 'main':
seq.c:6: warning: statement with no effect
[cdalten@localhost ~]$ ./seq
The value is: 256
However, when I change x from x>>8 to x++

#include <stdio.h>

int main(void)
{
int x = 256;
x++;

printf("The value is: %d\n", x);
return 0;
}

I get:

[cdalten@localhost ~]$ gcc -g -Wall seq.c -o seq
[cdalten@localhost ~]$ ./seq
The value is: 257

The question is, how come something like x>>8 discards the value right
away, but x++ doesn't?

Jun 6 '07 #1
13 1241
Chad said:

<snip>
The question is, how come something like x>>8 discards the value right
away, but x++ doesn't?
It does. The difference is that x++ has a side-effect, which x>>8
doesn't, and the compiler considers it plausible that you wrote x++ not
for its value but for its side-effect, so it doesn't produce a
diagnostic message in the x++ case.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 6 '07 #2
Chad wrote:

int main(void)
{
int x = 256;
x>>8;

printf("The value is: %d\n", x);
return 0;
}

I get:

[cdalten@localhost ~]$ gcc -g -Wall seq.c -o seq
seq.c: In function 'main':
seq.c:6: warning: statement with no effect
The shift operation `x >8` takes the value of `x`,
shifts it 8 places to the right, and throws the
result away.
int main(void)
{
int x = 256;
x++;

printf("The value is: %d\n", x);
return 0;
}
The post-increment operation `x++` delivers (and discards)
the original value of `x`, and also arranges that `x`
is incremented.
The question is, how come something like x>>8 discards the value right
away, but x++ doesn't?
`x++` /does/ discard its value right away, the same as `x>>8` does.
However, the /increment operation/ is a side-effect, not a value,
and happens regardless.

--
"There's a doorway where there was a wall" /Master Humpries Clock/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 6 '07 #3
In article <11**********************@o5g2000hsb.googlegroups. com>,
Chad <cd*****@gmail.comwrote:
>The question is, how come something like x>>8 discards the value right
away, but x++ doesn't?
x>>8 is just like x+1 or x/2 - it doesn't change the value of x.
It gives you the value of 8, shifted right 8 places. It doesn't
shift x itself.

x>>=8 does what I think you are expecting.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 6 '07 #4
What's the rationale behind having something like x>>8 not having a
side-effect, but somthing like, x++ having a side effect?
Jun 6 '07 #5
In article <11**********************@p77g2000hsh.googlegroups .com>,
Chad <cd*****@gmail.comwrote:
>What's the rationale behind having something like x>>8 not having a
side-effect, but somthing like, x++ having a side effect?
Why do you think that x>>8 should be like x++? Is it because of
the repeated >? In fact >is like +, -, *, and /, none of which
have side effects.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 6 '07 #6
Chad wrote:
What's the rationale behind having something like x>>8 not having a
side-effect, but somthing like, x++ having a side effect?
`x >8` is like `x + 1`. It has no side-effect because if it did,
expressions would be updating their operands all over the place.
Ikk.

`x++` has a side-effect because that's what it's /for/; to provide
the value of a variable and to update it, to make common combinations
of operations more compact (and maybe, in the old days, more efficient).

An expression like `a[i++]` allows you to get at an element of an
array /and/ advance the index to the next position, all in one go.
Otherwise you'd have to find somewhere to put the increment, `i += 1`.

Opinions on whether this kind of elegant compactness is a good idea
are rumoured to vary. As with most programming languages features,
it's possible to overdo things, and it's possible to misunderstand
what such expressions actually /mean/ and where the language crouches
ready to pull the rug out from under your feet, giggling like an
insane ferret on nitrous oxide.

--
The shortcuts are all full of people using them.

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 6 '07 #7
On 6 Jun, 14:38, Chris Dollin <chris.dol...@hp.comwrote:
[snip]
>... giggling like an insane ferret on nitrous oxide.
What a charming image - may I reuse it, please?

Jun 6 '07 #8
Chad wrote:
>
.... snip ...
>
The question is, how come something like x >8 discards the value
right away, but x++ doesn't?
Because you failed to store the value of the expression. Read up
on expressions, and on the action of the ++ operator.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 6 '07 #9
Chris Dollin <ch**********@hp.comwrites:
Chad wrote:
>int main(void)
{
int x = 256;
x>>8;

printf("The value is: %d\n", x);
return 0;
}

I get:

[cdalten@localhost ~]$ gcc -g -Wall seq.c -o seq
seq.c: In function 'main':
seq.c:6: warning: statement with no effect

The shift operation `x >8` takes the value of `x`,
shifts it 8 places to the right, and throws the
result away.
[...]

Um, that's not quite the way I'd put it.

The expression x>>8 yields the value of x right-shifted by 8 bits.
Throwing away the result isn't a feature of the ">>" operator; the
result is thrown away because you (the OP) asked for it to be thrown
away, by using the expression as a statement (by adding the ';').

What x>>8 *doesn't* do is modify the value of x, just as the
equivalent x/256 doesn't modify the value of x. Similarly, 256>>8
doesn't modify the value of 256, and 2+3 doesn't modify the value of 2
or 3.

The ++ operator, as numerous others have pointed out, has the *side
effect* of modifying the object that is its operand. That's why the
++ operator, unlike the >operator, can *only* be applied to an
object (an lvalue); 256++ is illegal.

If you *want* to modify the value of x, replacing it with the result
of x>>8, you can write:

x = x>>8;

or, equivalently:

x >>= 8;

A couple of notes on the original program:

You're missing the "#include <stdio.h>". Your program may happen to
appear to work without it, but it's mandatory if you use printf or
anything else declared in <stdio.h>. (Somebody might point out that
you could drop the #include and declare the printf function yourself;
that's true, but it's a dumb thing to do.)

You can use bitwise operators, (<<, >>, &, |, ^) on signed integers if
you really want to, but it almost always makes much more sense to
apply them to unsigned integers. There are rules on how these
operators work on negative values, but I can't be bothered to look
them up.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 6 '07 #10
On Jun 6, 6:22 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <1181136264.990367.254...@p77g2000hsh.googlegroups .com>,

Chad <cdal...@gmail.comwrote:
What's the rationale behind having something like x>>8 not having a
side-effect, but somthing like, x++ having a side effect?

Why do you think that x>>8 should be like x++? Is it because of
the repeated >? In fact >is like +, -, *, and /, none of which
have side effects.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Today, somewhere between me considering if I should make a third
attempt to apply to UC-Berkeley and my manager at work asking me if I
was dumb, the whole x>>8 vs x++ sank in. Then as the accounting lady
at work was making more sexual advances at me, I realized I could have
saved myself posting on here had I given the whole x>>8 vs x++ more
than a millisecond of thought before giving up.

Jun 7 '07 #11
Chad wrote:
>
.... snip ...
>
Today, somewhere between me considering if I should make a third
attempt to apply to UC-Berkeley and my manager at work asking me
if I was dumb, the whole x>>8 vs x++ sank in. Then as the
accounting lady at work was making more sexual advances at me, I
realized I could have saved myself posting on here had I given
the whole x>>8 vs x++ more than a millisecond of thought before
giving up.
Ah, a truly wise diatribe. You will go far.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 7 '07 #12
ma**********@pobox.com wrote:
On 6 Jun, 14:38, Chris Dollin <chris.dol...@hp.comwrote:
[snip]
>>... giggling like an insane ferret on nitrous oxide.

What a charming image - may I reuse it, please?
Certainly. No attribution required (but appreciated if present).

--
"It was the first really clever thing the King had said that day."
/Alice in Wonderland/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 7 '07 #13
Keith Thompson wrote:
Chris Dollin <ch**********@hp.comwrites:
>Chad wrote:
>>int main(void)
{
int x = 256;
x>>8;

printf("The value is: %d\n", x);
return 0;
}

I get:

[cdalten@localhost ~]$ gcc -g -Wall seq.c -o seq
seq.c: In function 'main':
seq.c:6: warning: statement with no effect

The shift operation `x >8` takes the value of `x`,
shifts it 8 places to the right, and throws the
result away.
[...]

Um, that's not quite the way I'd put it.

The expression x>>8 yields the value of x right-shifted by 8 bits.
Throwing away the result isn't a feature of the ">>" operator; the
result is thrown away because you (the OP) asked for it to be thrown
away, by using the expression as a statement (by adding the ';').
Ooof. You're quite right, Keith; I was sloppy. Thanks for the catch.

--
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 7 '07 #14

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

Similar topics

37
by: Jon Perez | last post by:
I saw this code snippet: sock.listen(20) for _ in range(20): newsock, client_addr = sock.accept() print "Client connected:", client_addr data = "" why use _ for this example? Is there any...
40
by: Zach | last post by:
Can someone please explain what this means and illustrate the difference with some code. Thanks, Zach
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
6
by: raoul | last post by:
MSIE 6.0 apparently does not support protyping with objects created with document.createElement, while Firefox does. I tested it by typing it into the adress bar, but it also appears to be the...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
14
by: matevzb | last post by:
The C99 standard describes snprintf() in an awkward way, so it's hard (for me) to assume what the result will be in some situations. 1. The "Description" section states: "... Otherwise, output...
30
by: lovecreatesbea... | last post by:
K&R says the following in the preface to the first edition, "... the C compiler, and ... are written in C." I'm wondering, does it say even the first / original C compiler was written in C?
5
by: sillyr | last post by:
Hi I would like to add into the SQL statement that when there was no value selected put in a 0. I looked at another example on the forum that said to use this Nz(., 0) but I didn't have any luck....
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.