View previous topic :: View next topic |
Author |
Message |
weg22
Joined: 08 Jul 2005 Posts: 91
|
signed ints? |
Posted: Tue Aug 02, 2005 2:33 pm |
|
|
Hi all,
When defining an int in ccs, is it just capable of taking values from 0-255? Is it not able to take negative numbers? What would the value of the following be:
int x;
x = -8;
Would that give you zero or -8 for x?
Thanks! |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Aug 02, 2005 2:41 pm |
|
|
An int and a signed int store numbers the same - the difference is in how the variables are treated.
If I did this:
int x = 10, y = -1;
x would contain decimal 10, or hex 0x0A. y would contain decimal 255, or hex 0xff.
If I had defined both x & y as being unsigned int, their contents would be exactly the same. The only difference would be in doing arithmetic or in doing tests. For instance, if they were both defined as ints, then:
if (y < x) would never evaluate to true, since y = 255, and x = 10.
If they were both defined as signed ints, then if (y < x) would evaluate to true. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 02, 2005 3:21 pm |
|
|
In CCS, integer declarations default to unsigned values. This is
different from normal C. To declare an unsigned 8-bit integer,
you can do this:
int my_value;
or
int8 my_value;
In CCS, to declare a signed 8-bit integer, you have to
explicitly declare as such. Example:
signed int my_value;
or
signed int8 my_value; |
|
|
|