Go to Post ...because "someone on ChiefDelphi said so," doesn't fly when your robot can't pass inspection. - Madison [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 21-02-2004, 18:47
Jeff McCune's Avatar
Jeff McCune Jeff McCune is offline
Alpha Geek
#0677 (The Wirestrippers)
Team Role: Mentor
 
Join Date: Jan 2003
Location: The Ohio State University
Posts: 67
Jeff McCune is on a distinguished road
Send a message via ICQ to Jeff McCune Send a message via AIM to Jeff McCune
Integer Representation - Huh?!

Not sure if I missed something in my CIS classes, but can someone offer some insight into this behavior?
Code:
int foo;
foo = 127 + 127;
printf ("%16b | %d \r\n", foo, foo);
// prints: "1111 1111 1111 1110 | -2"
Is this a bug in the printf code, or is the compiler and microchip actually representing this using ones for the first 15 significant digits and a zero at the least significant position?

It would seem that it might be treating the literal values as an 8 bit integer and then padding with the most significant digit to get to 16 bits. "it" may be printf, or the compiler/CPU... ?

This code is my work around:
Code:
unsigned char foo;
foo = 127 + 127;
printf ("%16b | %d \r\n", (int)foo, (int)foo);
// prints: "0000 0000 1111 1110 | 254"
 
// This also works, keeping all my data types at (int)
// This also implies it's a bug in how the chip/compiler represents literal values
int foo;
foo = (int)(unsigned char)(127 + 127);
printf("%16b | %d \r\n", foo, foo);
From the last example, it would seem that literal values are padded out using the most significant digit, but this padding doesn't occur if you cast the data along the way. Am I correct in this guess?

Any insights? I'd like to just use one data type (int) everywhere, but this is hanging me up, since it becomes difficult to debug the output.
__________________
Team 677 - The Wirestrippers - Columbus School for Girls and The Ohio State University
EMAIL: mccune@ling.ohio-state.edu

...And all you touch and all you see
Is all your life will ever be...

Last edited by Joe Johnson : 21-02-2004 at 21:57. Reason: Typo in the Title
  #2   Spotlight this post!  
Unread 21-02-2004, 19:23
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: Interger Representation - Huh?!

Quote:
Originally Posted by Jeff McCune
Not sure if I missed something in my CIS classes, but can someone offer some insight into this behavior?
Code:
int foo;
foo = 127 + 127;
printf ("%16b | %d \r\n", foo, foo);
// prints: "1111 1111 1111 1110 | -2"
Is this a bug in the printf code, or is the compiler and microchip actually representing this using ones for the first 15 significant digits and a zero at the least significant position?
The compiler has a documented non-standard behavior that may relate to this. See below, from the C18 compiler reference manual (available on the Microchip site).

2.7 ISO DIVERGENCES

2.7.1 Integer Promotions
ISO mandates that all arithmetic be performed at int precision or greater. By default, MPLAB C18 will perform arithmetic at the size of the largest operand, even if both operands are smaller than an int. The ISO mandated behavior can be instated via the -Oi command-line option.

For example:

Code:
unsigned char a, b; 
unsigned i;
a = b = 0x80;
i = a + b; /* ISO requires that i == 0x100, but in C18 i == 0 */
Note that this divergence also applies to constant literals. The chosen type for constant literals is the first one from the appropriate group that can represent the value of the constant without overflow.

For example:

Code:
#define A 0x10 /* A will be considered a char unless -Oi specified */
#define B 0x10 /* B will be considered a char unless -Oi specified */
#define C (A) * (B)
unsigned i;
i = C; /* ISO requires that i == 0x100, but in C18 i == 0 */

=====

It is confusing. I think you could cast the literal as an (int):

Code:
foo = (int)127 + (int)127;
Or maybe this?

Code:
foo = 127L + 127L;
I haven't tried these.

Last edited by Joe Johnson : 21-02-2004 at 21:58.
  #3   Spotlight this post!  
Unread 21-02-2004, 19:24
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Interger Representation - Huh?!

just make sure you cast all of the variables you pass to printf as (int).
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)

Last edited by Joe Johnson : 21-02-2004 at 21:59.
  #4   Spotlight this post!  
Unread 21-02-2004, 19:47
Jeff McCune's Avatar
Jeff McCune Jeff McCune is offline
Alpha Geek
#0677 (The Wirestrippers)
Team Role: Mentor
 
Join Date: Jan 2003
Location: The Ohio State University
Posts: 67
Jeff McCune is on a distinguished road
Send a message via ICQ to Jeff McCune Send a message via AIM to Jeff McCune
Re: Interger Representation - WTF?

gnormhurst,
Thank you for that incredibly detailed, quick, and accurate post. I'll make sure to read the C18 tech docs as soon as I can find the time. So far I've been very happy with how compliant the compiler is. I think you hit the nail on the head.

I hacked a macro to manipulate the datatypes I wanted to work with, but I *hate* macros. I think I might just use the command line option you mentioned, after I research it's overall implications.

deltacoder1020,
If you look over my examples, the type of data I was passing to printf was already int. re-casting it inside the printf statement won't change anything.
__________________
Team 677 - The Wirestrippers - Columbus School for Girls and The Ohio State University
EMAIL: mccune@ling.ohio-state.edu

...And all you touch and all you see
Is all your life will ever be...
  #5   Spotlight this post!  
Unread 21-02-2004, 21:45
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Interger Representation - Huh?!

sry about that, perhaps I was unclear - essentially, MCC18 casts any constant from 0-255 as a unsigned char, so you may want to cast the constants as well.
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)

Last edited by Joe Johnson : 21-02-2004 at 22:00.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 00:05.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi