|
|
View previous topic :: View next topic |
Author |
Message |
roby Guest
|
understanding signed long |
Posted: Wed May 31, 2006 1:01 pm |
|
|
I would like to know how many bits a signed long variable occupies and if it is possible to transfer that information through I2C bus.
My code is:
// master
signed long count
i2c_write (make8 (count,0))
i2c_write (make8 (count.1))
// slave
signed long counter
int8 counter_high, counter_low
counter_low=i2c_read()
counter_high=i2c_read()
counter=make16(counter_high, counter_low)
Thank you |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 31, 2006 4:04 pm |
|
|
Quote: | I would like to know how many bits a signed long variable occupies |
Download the CCS compiler manual. Go to page 86 in the Acrobat reader.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
There is a chart that tells you the size of a 'long' variable is in CCS.
The size is the same for a 'signed' or unsigned long. |
|
|
roby Guest
|
the essential question |
Posted: Thu Jun 01, 2006 12:43 am |
|
|
I have read the manual, but there is a thing I can't understand
The information I need is if I can use make8 and make16 with signed long and not loose the sign.
I do not know if I explain the problem
Thank you |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Jun 01, 2006 9:30 am |
|
|
Quote: |
I have read the manual, but there is a thing I can't understand
|
In CCS unless it indicates the opposite in explicit form, an int by default is an unsigned integer.
Anyway if you are using signed int, the MSBit of the MSByte should keep the sign.
Humberto |
|
|
Ttelmah Guest
|
Re: the essential question |
Posted: Thu Jun 01, 2006 10:09 am |
|
|
roby wrote: | I have read the manual, but there is a thing I can't understand
The information I need is if I can use make8 and make16 with signed long and not loose the sign.
I do not know if I explain the problem
Thank you |
Nothing will be lost. The function 'make 8', just returns the byte (whichever one you select), and couldn't care at all whether the top bit is a sign or not. Similarly, make16, just puts two bytes back together.
If you have two bytes:
And you assemble together into a variable, you have defined as unsigned int16, then this variable will have the value '32887'. Put the two bytes into a variable you have designated as 'signed', and it will have the value
-32649.
You can play with this, and get a better understanding of it all (hopefully... :-), by using a union. If you declare:
[code]
union types {
int16 word;
signed int16 sword;
int8 b[2];
} converter;
[code]
Then set (say) converter.sword = -1000, you can access the two bytes seperately, as converter.b[0], and converter.b[1], which will be 0x18, and 0xFC (this basically does the same as 'make8'). You can also access 'converter.word', which will equal 64536. The same data, has different meanings, according to how you 'look' at it. All the 'signed' declaration does, is say to the compiler, to use the top bit as a 'sign' bit. It does nothing tochange the actual naure of the values.
Using the union, can also be done the other way, so if you write values into 'converter.b[0]', and 'converter.b[1]', you can then look at what this pair of bytes represent, when treated as a signed, or unsigned value (using converter.sword, and converter.word).
Best Wishes |
|
|
|
|
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
|