Go to Post Don't put sprockets in an empty pizza box and then throw it into the garbage. Something we learned from experience. - Herodotus [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 05-01-2008, 22:23
dmlawrence dmlawrence is offline
MIT '14
FRC #1751 (Warriors)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Long Island
Posts: 63
dmlawrence is an unknown quantity at this point
Question Signed and unsigned variables

What is the proper method to cast from an unsigned to a signed variable?

For example, suppose that u is an unsigned int and i is a signed int.

When u is 0, i should be -32768.
When u is 32768, i should be 0.
When u is 65535, i should be 32767.

Which of the following is correct:
i = (unsigned int) u or
i = (unsigned int) (u - 32768)?

Thanks,
David
  #2   Spotlight this post!  
Unread 05-01-2008, 22:34
mluckham's Avatar
mluckham mluckham is offline
Registered User
FRC #0758 (Sky Robotics)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2006
Location: Ontario, Canada
Posts: 116
mluckham will become famous soon enoughmluckham will become famous soon enough
Re: Signed and unsigned variables

Code:
When u is 0, i should be -32768.
When u is 32768, i should be 0.
When u is 65535, i should be 32767.

Which of the following is correct:
i = (unsigned int) u or
i = (unsigned int) (u - 32768)?

As a matter of form, neither is correct, although the second is closest to your requirements. Assuming you have defined 'u' as unsigned int, and 'i' as int, you use:

i = (int) (u - 32768);

Last edited by mluckham : 05-01-2008 at 22:54.
  #3   Spotlight this post!  
Unread 05-01-2008, 22:40
dmlawrence dmlawrence is offline
MIT '14
FRC #1751 (Warriors)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Long Island
Posts: 63
dmlawrence is an unknown quantity at this point
Re: Signed and unsigned variables

Thanks!

-David
  #4   Spotlight this post!  
Unread 05-01-2008, 22:42
Foster Foster is offline
Engineering Program Management
VRC #8081 (STEMRobotics)
Team Role: Mentor
 
Join Date: Jul 2007
Rookie Year: 2005
Location: Delaware
Posts: 1,385
Foster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond repute
Re: Signed and unsigned variables

int main(int argc, char *argv[])
{
unsigned int u;
int i;

u = 0; // i should be -32768.
i = (unsigned int) u;
printf("%d %u\n",i , u);
i = (unsigned int) (u - 32768);
printf("%d %u\n",i , u);

u = 32768; // i should be 0.
i = (unsigned int) u;
printf("%d %u\n",i , u);
i = (unsigned int) (u - 32768);
printf("%d %u\n",i , u);

u=65535; // i should be 32767.
i = (unsigned int) u;
printf("%d %u\n",i , u);
i = (unsigned int) (u - 32768);
printf("%d %u\n",i , u);
return 0;
}

0 0
-32768 0

32768 32768
0 32768

65535 65535
32767 65535

Your test case with output. Did that help?
  #5   Spotlight this post!  
Unread 05-01-2008, 23:03
colt527 colt527 is offline
Registered User
AKA: Ken Colton
FRC #0527
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Long Island
Posts: 123
colt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to beholdcolt527 is a splendid one to behold
Send a message via AIM to colt527
Re: Signed and unsigned variables

My first attempt at this post did not address the problem ... so second attempt.

The only thing you need to be careful of is that casting is not a shift as it might seem intuitive to be. An unsigned char 0 is not equivalent to a signed char 128. Negative numbers are stored in 2s compliment form. Which means the binary for -1 is:
111111111 11111111

NOT: 10000000 0000000, which might seem to make more sense at first.
They use 2s compliment because it makes all of the standard binary operations work for negative numbers.

If you cast a signed int -1 to an unsigned int, you will get 65,535. When you cast from unsigned to signed it does not touch the binary of the variable, just how its interpreted. Just like how you can print the signed interpretation of an unsigned variable using %d in printf instead of %u.

The best way to understand whats happening when u cast unsigned to signed you really need to look up information on 2s compliment.

If you HAD to perform a shift, you would need to do it manually. Ex:

shiftedUnsignedVar = (unsigned int)(signedVar + 32768); or
shiftedSignedVar = (int)(unsignedVar - 32768);

As showed in fosters output that will match the example that you gave to start with.

Hope that helped,
Ken
__________________
Mentor, Team 527 -- Plainedge Red Dragons
FIRST Volunteer
SUNY Stony Brook Computer Science 2010
kcolton@gmail.com

Last edited by colt527 : 05-01-2008 at 23:14. Reason: Added example
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
pic: Dean and Woodie Signed Saftey Glass andrew348 Extra Discussion 8 27-03-2007 16:36
Labview dashboard and multiple variables Joe Ross LabView and Data Acquisition 2 15-03-2006 21:55
Math.h and Functions and Variables amateurrobotguy Programming 1 26-02-2005 03:19
problems assigning unsigned chars in FRC Code LoyolaCubs Programming 6 26-01-2004 23:21
My team is registered and signed up for Rutgers(rookie)! Erodge General Forum 5 11-10-2002 10:13


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

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