Go to Post The greatest lesson we can teach our kids is how to win. - AdamHeard [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 22-03-2004, 14:02
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
What the...? casting bug?

'splain this, somebody:

Code:
#define STRAIGHT_DRIVE_DIRECTION ( 127 + 30 )
 
      static int steeringValue = 0;
      . . .
      steeringValue = (int) ((STRAIGHT_DRIVE_DIRECTION));
      printf( "nav: DTC  %3d ", steeringValue  );
That printed a value of -99! Why?

Changing the assignment to:
Code:
      steeringValue = (int) ((unsigned char) (STRAIGHT_DRIVE_DIRECTION));
made it print "157" as expected the first time.

Is this weird, or is there something about type casting I don't understand?
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
  #2   Spotlight this post!  
Unread 22-03-2004, 14:08
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: What the...? casting bug?

Ohhhhhhh!

Funny how you understand something as soon as you post it.

The compiler takes (127 + 30) and performs signed 8-bit arithmetic, yielding -99. Casting as a signed (int) duly kept the sign: -99. Casting as an unsigned char reinterpreted the value as 157, which was then cast as a signed int: 157.

My error was in assuming that constants were treated as unsigned chars, but they are treated as signed chars.
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
  #3   Spotlight this post!  
Unread 22-03-2004, 14:18
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: What the...? casting bug?

Quote:
Originally Posted by gnormhurst
Ohhhhhhh!

Funny how you understand something as soon as you post it.

The compiler takes (127 + 30) and performs signed 8-bit arithmetic, yielding -99. Casting as a signed (int) duly kept the sign: -99. Casting as an unsigned char reinterpreted the value as 157, which was then cast as a signed int: 157.

My error was in assuming that constants were treated as unsigned chars, but they are treated as signed chars.
That is exactly what happened.
__________________

  #4   Spotlight this post!  
Unread 22-03-2004, 14:20
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: What the...? casting bug?

Boy, you guys are fast.

Norm, I think if you had used
Code:
#define STRAIGHT_DRIVE_DIRECTION ( 127u + 30 )
you would have been ok.
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
  #5   Spotlight this post!  
Unread 22-03-2004, 15:49
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: What the...? casting bug?

There's a compuler option for that. Project>Build Options...>Project. Go to MPLAB C18. check "Treat 'char' as unsigned" (under Category General). The command-line flag is '-k'
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
printf bug - beware! WizardOfAz Programming 8 10-03-2004 09:50
bug? Duke 13370 CD Forum Support 4 30-12-2003 21:44
Gingery Lathe - Metal Sand Casting sanddrag General Forum 13 14-08-2003 21:41
Last Call for bug reports rbayer Programming 0 08-01-2003 02:48
Small bug Jay Lundy CD Forum Support 1 18-06-2001 17:03


All times are GMT -5. The time now is 03:08.

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