* code questions.
@ 2008-06-03 1:48 Scott Phuong
2008-06-03 1:59 ` me22
0 siblings, 1 reply; 6+ messages in thread
From: Scott Phuong @ 2008-06-03 1:48 UTC (permalink / raw)
To: gcc-help
Hi GCC Help,
What is my confusion here? I am using gcc 4.1.2-14 on an i386 machine.
I don't know why I am getting a different value than I expect. Is
there a compiler flag I can use to make the expression evaluate
correctly according to my expectations? Thanks!
int main(char argc, char* agrv[]) {
unsigned short a;
unsigned short b;
a = 0xFFFF;
b = 0x3FC;
a = (a + 1) % b;
printf ("A is 0x%x\n", a);
// I expect the answer to be 0 and it is not! It is 0x100. Why is this?
a = 0xFFFF;
a = (a + 1);
printf ("A is 0x%x\n", a);
// I expect the answer to be 0 and it is.
a = a % b;
printf ("A is 0x%x\n", a);
// I expect the answer to be 0 and it is.
}
Best Regards,
Scott
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: code questions.
2008-06-03 1:48 code questions Scott Phuong
@ 2008-06-03 1:59 ` me22
2008-06-03 2:04 ` Scott Phuong
0 siblings, 1 reply; 6+ messages in thread
From: me22 @ 2008-06-03 1:59 UTC (permalink / raw)
To: Scott Phuong; +Cc: gcc-help
On Mon, Jun 2, 2008 at 9:48 PM, Scott Phuong <mycleanjunk@gmail.com> wrote:
>
> unsigned short a;
> unsigned short b;
>
> a = 0xFFFF;
> b = 0x3FC;
>
> a = (a + 1) % b;
> printf ("A is 0x%x\n", a);
> // I expect the answer to be 0 and it is not! It is 0x100. Why is this?
>
I bet if you did
++a; a %= b;
you'd get 1.
I don't know the exact rules, but 1 is an int, so a+1 will give you
(int)a + 1, which will be 0x10000, which when modded by (int)b will
not be 0.
I'd have to read up on integral promotion to be sure, but I think the
only way is to add an explicit cast.
a = (unsigned short)(a+1) % b;
(If you want to keep it one expression, and of that form. ++a, a %=b;
is possible, as mentioned, and a = ( (a+1) & 0xFFFFu ) % b would also
work.)
HTH,
~ Scott
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: code questions.
2008-06-03 1:59 ` me22
@ 2008-06-03 2:04 ` Scott Phuong
2008-06-03 2:11 ` me22
0 siblings, 1 reply; 6+ messages in thread
From: Scott Phuong @ 2008-06-03 2:04 UTC (permalink / raw)
To: me22; +Cc: gcc-help
Hi,
Thanks so much for your quick reply. This code is simliar to code in a
third party source code that I have to fix and they do this all over
the place. The question is w/o touching their code is there a way I
can fix this? I tried explicitly casting the 1 to an unsigned short
but it doesn't seem to work like this:
a = (a + (unsigned short) 1) % b.
Thanks for your help.
Scott
On Mon, Jun 2, 2008 at 6:59 PM, me22 <me22.ca@gmail.com> wrote:
> On Mon, Jun 2, 2008 at 9:48 PM, Scott Phuong <mycleanjunk@gmail.com> wrote:
>>
>> unsigned short a;
>> unsigned short b;
>>
>> a = 0xFFFF;
>> b = 0x3FC;
>>
>> a = (a + 1) % b;
>> printf ("A is 0x%x\n", a);
>> // I expect the answer to be 0 and it is not! It is 0x100. Why is this?
>>
>
> I bet if you did
>
> ++a; a %= b;
>
> you'd get 1.
>
> I don't know the exact rules, but 1 is an int, so a+1 will give you
> (int)a + 1, which will be 0x10000, which when modded by (int)b will
> not be 0.
>
> I'd have to read up on integral promotion to be sure, but I think the
> only way is to add an explicit cast.
>
> a = (unsigned short)(a+1) % b;
>
> (If you want to keep it one expression, and of that form. ++a, a %=b;
> is possible, as mentioned, and a = ( (a+1) & 0xFFFFu ) % b would also
> work.)
>
> HTH,
> ~ Scott
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: code questions.
2008-06-03 2:04 ` Scott Phuong
@ 2008-06-03 2:11 ` me22
2008-06-03 2:31 ` Scott Phuong
0 siblings, 1 reply; 6+ messages in thread
From: me22 @ 2008-06-03 2:11 UTC (permalink / raw)
To: Scott Phuong; +Cc: gcc-help
On Mon, Jun 2, 2008 at 10:04 PM, Scott Phuong <mycleanjunk@gmail.com> wrote:
> Thanks so much for your quick reply. This code is simliar to code in a
> third party source code that I have to fix and they do this all over
> the place. The question is w/o touching their code is there a way I
> can fix this?
Switch to a platform with 16-bit ints?
> I tried explicitly casting the 1 to an unsigned short
> but it doesn't seem to work like this:
>
> a = (a + (unsigned short) 1) % b.
>
More evidence that you're hitting integral promotion, so there's
basically nothing you can do about it.
(Besides make sure a is never that large :P)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: code questions.
2008-06-03 2:11 ` me22
@ 2008-06-03 2:31 ` Scott Phuong
2008-06-03 13:56 ` Tony Wetmore
0 siblings, 1 reply; 6+ messages in thread
From: Scott Phuong @ 2008-06-03 2:31 UTC (permalink / raw)
To: me22; +Cc: gcc-help
I see. Well, thank you very much for your help. I appreciate the quick response.
Scott
On Mon, Jun 2, 2008 at 7:11 PM, me22 <me22.ca@gmail.com> wrote:
> On Mon, Jun 2, 2008 at 10:04 PM, Scott Phuong <mycleanjunk@gmail.com> wrote:
>> Thanks so much for your quick reply. This code is simliar to code in a
>> third party source code that I have to fix and they do this all over
>> the place. The question is w/o touching their code is there a way I
>> can fix this?
>
> Switch to a platform with 16-bit ints?
>
>> I tried explicitly casting the 1 to an unsigned short
>> but it doesn't seem to work like this:
>>
>> a = (a + (unsigned short) 1) % b.
>>
>
> More evidence that you're hitting integral promotion, so there's
> basically nothing you can do about it.
>
> (Besides make sure a is never that large :P)
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: code questions.
2008-06-03 2:31 ` Scott Phuong
@ 2008-06-03 13:56 ` Tony Wetmore
0 siblings, 0 replies; 6+ messages in thread
From: Tony Wetmore @ 2008-06-03 13:56 UTC (permalink / raw)
To: Scott Phuong; +Cc: gcc-help
Scott,
I just tried the following:
a = (unsigned short) ( a + 1 ) % b;
which appears to work as you expect. I tried it using gcc 3.4.4 on
Windows (Cygwin).
Good luck!
--
Tony Wetmore
Raytheon Solipsys
Scott Phuong wrote:
> I see. Well, thank you very much for your help. I appreciate the quick response.
>
> Scott
>
> On Mon, Jun 2, 2008 at 7:11 PM, me22 <me22.ca@gmail.com> wrote:
>> On Mon, Jun 2, 2008 at 10:04 PM, Scott Phuong <mycleanjunk@gmail.com> wrote:
>>> Thanks so much for your quick reply. This code is simliar to code in a
>>> third party source code that I have to fix and they do this all over
>>> the place. The question is w/o touching their code is there a way I
>>> can fix this?
>> Switch to a platform with 16-bit ints?
>>
>>> I tried explicitly casting the 1 to an unsigned short
>>> but it doesn't seem to work like this:
>>>
>>> a = (a + (unsigned short) 1) % b.
>>>
>> More evidence that you're hitting integral promotion, so there's
>> basically nothing you can do about it.
>>
>> (Besides make sure a is never that large :P)
>>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-06-03 13:56 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-03 1:48 code questions Scott Phuong
2008-06-03 1:59 ` me22
2008-06-03 2:04 ` Scott Phuong
2008-06-03 2:11 ` me22
2008-06-03 2:31 ` Scott Phuong
2008-06-03 13:56 ` Tony Wetmore
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).