Subject: Re: bump operators. How do we size the '1'
From: John Sanguinetti (jws@forteds.com)
Date: Wed Aug 01 2001 - 10:46:29 PDT
At 6:14 PM -0700 7/31/01, Michael McNamara wrote:
>Actually, I am not at all interested in size here, but rather in what
>does the code mean.
>
>Given
>main(){
> int a, answer, b;
> a = 1;
> b = 2;
>
> answer = (a++) >> 1;
> printf("answer is %d a is %d\n", answer,a);
> answer = (a++ + b) >> 1;
> printf("answer is %d a is %d\n",answer,a);
>}
>
>You may be thinking that C would print
>answer is 1 a is 2
>answer is 2 a is 3
>
>but instead it prints:
>answer is 0 a is 2
>answer is 2 a is 3
>
>If it was coded as:
> answer = (++a) >> 1;
> printf("answer is %d a is %d\n", answer,a);
> answer = (++a + b) >> 1;
> printf("answer is %d a is %d\n",answer,a);
>
>You would get:
>answer is 1 a is 2
>answer is 2 a is 3
>
>That is because 'answer = (a++)' means assign 'a' to 'answer' and THEN
>increment a after the assignment in complete.
>
Isn't that what Superlog proposes? If not, it should.
This archive was generated by hypermail 2b28 : Wed Aug 01 2001 - 10:47:15 PDT