CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Quick Signed Integer Question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Matthew Aurand



Joined: 20 Dec 2007
Posts: 9

View user's profile Send private message

Quick Signed Integer Question
PostPosted: Mon Mar 02, 2009 11:55 am     Reply with quote

Hey guys,

I am trying to convert a signed int to an unsigned int. What is the format of a signed int? I can't find the info in the compiler manual...

I am using 32-bit ints.

The MSB I assume is the sign bit, so if I want to convert say '-1000' to '1000' all I need to do is clear the MSB?



Thanks!


-Matt
_________________
-Matthew A. Aurand
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 12:01 pm     Reply with quote

They are stored in two's complement format:
http://en.wikipedia.org/wiki/Two's_complement


The quick way to get the Absolute value (to code it) is to use the ABS()
function.
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 2:00 pm     Reply with quote

You could do:

Code:
signed int32 value = -1000;
value = -value;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 2:04 pm     Reply with quote

With that method, he has to know ahead of time that the value is negative.
Or, he has to add a test (an if statement) to test if it's negative before
he does your code.

With the ABS() function, it works on either a positive or negative value.
You don't need to test it before you run the function on it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group