main()
{
int a =100;
a = a>>32;
printf(" %d", a);
}
it prints "a" as 100 only....but I am expecting a = 0.....
can some one tell me the reason?
bye
Deepak\ 12 2498
DeepaK K C wrote: main() { int a =100; a = a>>32; printf(" %d", a); }
it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
It's a manifestation of undefined behaviour. (Some machines
only have a 5-bit shift count, for example.)
What happens if you turn your compiler warnings up?
--
Chris "electric hedgehog" Dollin
DeepaK K C wrote: main() { int a =100; a = a>>32; printf(" %d", a); }
it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
bye Deepak\
Fixed version of your program:
#include <stdio.h>
int main(void)
{
int a = 100;
a = a>>32;
printf(" %d\n", a);
return 0;
}
temp(558)$ gcc -Wall -o foo foo.c
foo.c: In function `main':
foo.c:6: warning: right shift count >= width of type
temp(559)$ foo
100
Do you compile with warnings on? Unless int is 64 bits, you
are invoking undefined behavior. Once you do that, what you
get is up to the vagaries of fate, in gcc apparently it does
nothing for shifts >= to the size, at least for the version
I am using. Here is what the standard says:
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.
-David
DeepaK K C wrote: main() { int a =100; a = a>>32; printf(" %d", a); }
it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
probably because (32 % 32 == 0). Other errors in your program:
int main(void)
main returns an int, say so. If you're not using any arguments,
say so.
failure to #include <stdio.h>
The call to printf is invalid, and leads to undefined behavior.
failure to return anything from main. return 0 needed.
failure to indent properly.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
CBFalconer wrote: DeepaK K C wrote:
main() { int a =100; a = a>>32; printf(" %d", a); }
it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason? probably because (32 % 32 == 0).
That's just (off-topic) speculation. Chris Dollin
has already provided the correct answer.
Other errors in your program:
int main(void)
main returns an int, say so. If you're not using any arguments, say so.
failure to #include <stdio.h>
The call to printf is invalid, and leads to undefined behavior.
It's only invalid in the absence of <stdio.h>, so I
think you're double-counting. (And I'm surprised you didn't
note the missing newline; it doesn't lead to U.B. but it's
an error nonetheless.)
failure to return anything from main. return 0 needed.
failure to indent properly.
Oh, come on. How about "uninformative identifiers"
or "inconsistent use of white space" or "lack of comments"
or "failure to use `>>=' when the opportunity arose?"
Not to mention "posting questions on a Wednesday" and
"running afoul of someone who arose on the wrong side
of bed?" Ease up a little.
-- Er*********@sun.com
Chris Dollin wrote: DeepaK K C wrote:
main() { int a =100; a = a>>32; printf(" %d", a); }
it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
It's a manifestation of undefined behaviour. (Some machines only have a 5-bit shift count, for example.)
More precisely, shifting by the full width of an integer, or
more, invokes undefined behaviour.
A 32-bit shift of a (non-negative) int can be performed in a
conforming way though...
a = a >> 15 >> 15 >> 2;
--
Peter
In article <11*********************@g14g2000cwa.googlegroups. com>,
"Peter Nilsson" <ai***@acay.com.au> wrote: Chris Dollin wrote: DeepaK K C wrote:
main() { int a =100; a = a>>32; printf(" %d", a); }
it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
It's a manifestation of undefined behaviour. (Some machines only have a 5-bit shift count, for example.)
More precisely, shifting by the full width of an integer, or more, invokes undefined behaviour.
A 32-bit shift of a (non-negative) int can be performed in a conforming way though...
a = a >> 15 >> 15 >> 2;
Much better to write:
a = (a >> 15) >> 15) >> 2;
Would you bet your life that you got your precedences right? Didn't
think so. Would I bet your life that you get your precedences right?
Absolutely.
Christian Bau wrote: In article <11*********************@g14g2000cwa.googlegroups. com>, "Peter Nilsson" <ai***@acay.com.au> wrote:[...] A 32-bit shift of a (non-negative) int can be performed in a conforming way though...
a = a >> 15 >> 15 >> 2;
Much better to write:
a = (a >> 15) >> 15) >> 2;
Ah, this is obviously some strange use of the phrase
"much better" that I wasn't previously aware of ...
-- Er*********@sun.com
Christian Bau wrote: "Peter Nilsson" <ai***@acay.com.au> wrote: A 32-bit shift of a (non-negative) int can be performed in a conforming way though...
a = a >> 15 >> 15 >> 2; Much better to write:
a = (a >> 15) >> 15) >> 2;
Goes to show: "If it ain't broken, don't fix it."
Would you bet your life that you got your precedences right?
Yes. Assignment operators have always had lower precedence than
shift operators. ;)
Obviously, you meant association rather than precedence, but even
there, yes! Why? Because in C++ I exploit the same left to right
association of shift operators all the time.
Ironic that C++ should make me a better C programmer, isn't it? ;)
--
Peter
In article <cv**********@news1brm.Central.Sun.COM>,
Eric Sosman <er*********@sun.com> wrote: Christian Bau wrote: In article <11*********************@g14g2000cwa.googlegroups. com>, "Peter Nilsson" <ai***@acay.com.au> wrote:[...] A 32-bit shift of a (non-negative) int can be performed in a conforming way though...
a = a >> 15 >> 15 >> 2;
Much better to write:
a = (a >> 15) >> 15) >> 2;
Ah, this is obviously some strange use of the phrase "much better" that I wasn't previously aware of ...
Maintainability. It is obvious to everybody what the intention of the
second version is, and what it does. It is impossible to determine what
the intention of the first version is (without additional comments in
the code), and I really have more important things to remember than
associativity of shift operators.
Christian Bau <ch***********@cbau.freeserve.co.uk> writes: In article <cv**********@news1brm.Central.Sun.COM>, Eric Sosman <er*********@sun.com> wrote:
Christian Bau wrote: > In article <11*********************@g14g2000cwa.googlegroups. com>, > "Peter Nilsson" <ai***@acay.com.au> wrote: >>[...] >>A 32-bit shift of a (non-negative) int can be performed in a >>conforming way though... >> >> a = a >> 15 >> 15 >> 2; > > > Much better to write: > > a = (a >> 15) >> 15) >> 2;
Ah, this is obviously some strange use of the phrase "much better" that I wasn't previously aware of ...
Maintainability. It is obvious to everybody what the intention of the second version is, and what it does. It is impossible to determine what the intention of the first version is (without additional comments in the code), and I really have more important things to remember than associativity of shift operators.
Um, you might want to count the parentheses in the second version.
--
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.
Eric Sosman wrote: Christian Bau wrote: "Peter Nilsson" <ai***@acay.com.au> wrote: [...] A 32-bit shift of a (non-negative) int can be performed in a conforming way though...
a = a >> 15 >> 15 >> 2;
Much better to write:
a = (a >> 15) >> 15) >> 2;
Ah, this is obviously some strange use of the phrase "much better" that I wasn't previously aware of ...
For any int size up to and including 32 bits, it is considerably
simpler, and even more efficient, to write:
a = 0;
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
On Thu, 17 Feb 2005 00:24:13 +0000, Christian Bau wrote:
.... Maintainability. It is obvious to everybody what the intention of the second version is, and what it does. It is impossible to determine what the intention of the first version is (without additional comments in the code), and I really have more important things to remember than associativity of shift operators.
I don't bother to remember all the precedence levels but associativity is
easy: all operators are left associative except unary, ternary and
assignment operators which are all right associative (according to the K&R
model). The only oddity is the function call operator which doesn't have a
fixed arity. That is left associative and still fits reasonably in the
rule (it isn't specifically a unary or ternary operator).
Lawrence This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Glen Able |
last post by:
Just to get my head straight on this...
Firstly, am I right in thinking that right-shifting a signed integer has an
undefined result (i.e. could be implemented as a logical or arithmetic
shift)?...
|
by: Mehta Shailendrakumar |
last post by:
Hello,
Can anyone suggest me operator to perform arithmetic shift in C?
May it be for a perticular compiler.
Thank you in advance.
Regards,
Shailendra
|
by: Simon Johnson |
last post by:
Is there an inbuilt circular shift operator in c#?
I've googled it but because c# happens to be musical note.. i get a
large noise to signal ratio :P
Simon.
|
by: ERE |
last post by:
Pardon me if this has been asked a million times -- but I thought that
because of generics, the right shift operator was supposed to change with C#
2.0. I thought it was supposed to change from...
|
by: Christian Christmann |
last post by:
Hi,
in the header of my class I've a constant
static const int a = ( 1 << 32 ) - 1;
When compiling the code, g++ issues the warning
"warning: left shift count >= width of type"
Why? And...
|
by: yuyang08 |
last post by:
Dear all,
Is there a logic shift operator in C++? I tried ">>", it is an
arithmetic shift operator.
Thanks!
-Andy
|
by: Nishu |
last post by:
Hi All,
Could you please explain whether C standard supports logical right
shift operation using some operator?
I know somewhere I read about >>operator. I thought, it is in C but i
think i'm...
|
by: Santosh Nayak |
last post by:
Hi,
Is there any way to catch the losing bit occurring due to Right Shift
Operator ?
e.g
int a = 5 ;
a = a >1 ; // // a is now 2 and the least significant bit is lost //
//
I want this...
|
by: knservis |
last post by:
Hi,
can somebody please explain to me what is the difference between right
(or left) shift >and right shift assign >>= ? Is it only
precidence?
Kosta
|
by: gaya3 |
last post by:
Hi,
can anyone explain the unsigned right shift operator with example?
And also why there is no unsigned left shift operator? is there any specific reason for that?
-Thanks & regards,
Hamsa
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
| |