signed char vs unsigned char

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi I know that
unsigned char is used to store the values from 0 to 255
signed char is used to store the values from -127 to 127

However what I found out is that memory representation of both of those types is the same.
i.e. if I write the following code and see the memory window
<span style="white-space:pre unsigned char unsigne[10];<br/>
<span style="white-space:pre unsigne[0] = 0;<br/>
<span style="white-space:pre unsigne[1] = 1;<br/>
<span style="white-space:pre unsigne[2] = -1;<br/>
<span style="white-space:pre unsigne[3] = 2;<br/>
<span style="white-space:pre unsigne[4] = -2;<br/>
<br/>
<span style="white-space:pre signed char signe[10];<br/>
<span style="white-space:pre signe[0] = 0;<br/>
<span style="white-space:pre signe[1] = 1;<br/>
<span style="white-space:pre signe[2] = -1;<br/>
<span style="white-space:pre signe[3] = 2;<br/>
<span style="white-space:pre signe[4] = -2;

then the memory of both of these pointers contain
00 01 ff 02 fe

I was thinking the memory of signed char would like, if the MSB is the sign bit.
00 01 81 02 82
meaning -1 = 0x81 = 1000 0001
meaning -2 = 0x81 = 1000 0002

View the full article
 
Back
Top